Hi, summarizing I have two pseudo-variables:
$var(allowed_zones) = 5; $var(id_zone) = 2;
The binary representation of 5 (3+2), assuming 8 bits lenght, is:
00000101
so bit 0 = 1, bit 1 = 0, bit 2 = 1 ...
Now I just need to know if bit number 2 ($var(id_zone)) of $var(allowed_zones) is 0 or 1.
Is there any function like isflagset() but for pseudo-variables?
Thanks a lot.
2011/6/16 Iñaki Baz Castillo ibc@aliax.net:
Now I just need to know if bit number 2 ($var(id_zone)) of $var(allowed_zones) is 0 or 1.
Is there any function like isflagset() but for pseudo-variables?
I've found isavpflagset() right now:
http://sip-router.org/wiki/cookbooks/core-cookbook/devel#isavpflagset
But it has no description. How is it supposed to work?
2011/6/16 Iñaki Baz Castillo ibc@aliax.net:
I've found isavpflagset() right now:
http://sip-router.org/wiki/cookbooks/core-cookbook/devel#isavpflagset
But it has no description. How is it supposed to work?
The Changelog file says:
----------------------------------- Every AVP may by flaged from script via setavpflag(avpid, flag) (it's similar like message flags setflags,resetflags,isflagset). AVP flags must be declared using avpflags statement. Flags may be tested via isavpflagset(avpid, flag). Both the setting and testing may also be processed in a module. It's currently used in the "rr" module for dialog_cookies. Such module should register flag using register_avpflag(flag_id).
Example: avpflags myflag, dialog_cookie;
$a = 123; setavpflag($a, "myflag");
if (isavpflagset($a, "myflag")) { ....
$dlg_foo = "foo"; $dlg_bar = "bar"; setavpflag("$f./^dlg_", "dialog_cookie"); ----------------------------------------------
But is says that I must use setavpflag() to set flags in the AVP. In my case, I retrieve the AVP value from a database (an integer field). Could it work?
Hello,
that function is probably for ser-like AVPs.
back to your original need, you can use directly if conditions like:
if($var(x) & 2) { ... }
will be true if the second bit is set. You have to use the power of two for the appropriate integer value to match the bit position. The master branch has bitwise left/right shift operations as well -- I was thinking of backporting since they were in 1.x but lost in integration of the core interpreter for 3.0, reported by Juha, so it is not really a new feature:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=508ad1c0...
Cheers, Daniel
On 6/16/11 1:15 PM, Iñaki Baz Castillo wrote:
2011/6/16 Iñaki Baz Castilloibc@aliax.net:
I've found isavpflagset() right now:
http://sip-router.org/wiki/cookbooks/core-cookbook/devel#isavpflagset
But it has no description. How is it supposed to work?
The Changelog file says:
Every AVP may by flaged from script via setavpflag(avpid, flag) (it's similar like message flags setflags,resetflags,isflagset). AVP flags must be declared using avpflags statement. Flags may be tested via isavpflagset(avpid, flag). Both the setting and testing may also be processed in a module. It's currently used in the "rr" module for dialog_cookies. Such module should register flag using register_avpflag(flag_id). Example: avpflags myflag, dialog_cookie; $a = 123; setavpflag($a, "myflag"); if (isavpflagset($a, "myflag")) { .... $dlg_foo = "foo"; $dlg_bar = "bar"; setavpflag("$f./^dlg_", "dialog_cookie");
But is says that I must use setavpflag() to set flags in the AVP. In my case, I retrieve the AVP value from a database (an integer field). Could it work?
2011/6/16 Daniel-Constantin Mierla miconda@gmail.com:
that function is probably for ser-like AVPs.
back to your original need, you can use directly if conditions like:
if($var(x) & 2) { ... }
will be true if the second bit is set. You have to use the power of two for the appropriate integer value to match the bit position.The master branch has bitwise left/right shift operations as well
Ok, so the above example if($var(x) & 2) { ... } will just work because the number 2 has the bit 2 with value 1 (well, in fact the bit 1 as the first one is the bit 0). But if I want to compare the bit 5 I need first to get 2^5 (for the second & operator) which I can get using bitwise left/right shift functions, am I right? (since there is no way in kamailio to calculate 2^N directly).
On Jun 16, 2011, at 2:03 PM, Iñaki Baz Castillo ibc@aliax.net wrote:
2011/6/16 Daniel-Constantin Mierla miconda@gmail.com:
that function is probably for ser-like AVPs.
back to your original need, you can use directly if conditions like:
if($var(x) & 2) { ... }
will be true if the second bit is set. You have to use the power of two for the appropriate integer value to match the bit position.The master branch has bitwise left/right shift operations as well
Ok, so the above example if($var(x) & 2) { ... } will just work because the number 2 has the bit 2 with value 1 (well, in fact the bit 1 as the first one is the bit 0). But if I want to compare the bit 5 I need first to get 2^5 (for the second & operator) which I can get using bitwise left/right shift functions, am I right? (since there is no way in kamailio to calculate 2^N directly).
You can do 2*2*2... to get the power of two. Doing it in a while loop gives dynamic power parameter. Other option is to use Lua for example.
... until the left shift gets in 3.1 - if you can test the patch from master and all goes fine, then it may happen faster :-)
Cheers, Daniel
2011/6/16 Daniel-Constantine Mierla miconda@gmail.com:
You can do 2*2*2... to get the power of two. Doing it in a while loop gives dynamic power parameter. Other option is to use Lua for example.
A bit exotic just to calculate a simple mathematical operation :)
... until the left shift gets in 3.1 - if you can test the patch from master and all goes fine, then it may happen faster :-)
We have it already working using that ;)