diff --git a/src/modules/math/doc/math_admin.xml b/src/modules/math/doc/math_admin.xml index e0051824c6f..e3695fefbaf 100644 --- a/src/modules/math/doc/math_admin.xml +++ b/src/modules/math/doc/math_admin.xml @@ -80,7 +80,7 @@ -
+
<function moreinfo="none">log(x, res)</function> @@ -97,7 +97,51 @@ ... log("10", "$var(res)"); $var(x) = 10; - pow("$var(x)", "$var(res)"); + log("$var(x)", "$var(res)"); +... + + +
+
+ + <function moreinfo="none">log2(x, res)</function> + + + The function computes the base-2 logarithm of x and stores the result + in res. + + + This function can be used from ANY_ROUTE. + + + <function>log2</function> usage + +... + log2("16", "$var(res)"); + $var(x) = 16; + log2("$var(x)", "$var(res)"); +... + + +
+
+ + <function moreinfo="none">log10(x, res)</function> + + + The function computes the base-10 logarithm of x and stores the result + in res. + + + This function can be used from ANY_ROUTE. + + + <function>log10</function> usage + +... + log10("100", "$var(res)"); + $var(x) = 100; + log10("$var(x)", "$var(res)"); ... diff --git a/src/modules/math/math_mod.c b/src/modules/math/math_mod.c index 6f2b2f17ec4..314bb23e9a5 100644 --- a/src/modules/math/math_mod.c +++ b/src/modules/math/math_mod.c @@ -37,6 +37,8 @@ MODULE_VERSION static int w_math_pow(sip_msg_t *msg, char *v1, char *v2, char *r); static int w_math_log(sip_msg_t *msg, char *v1, char *r); +static int w_math_log2(sip_msg_t *msg, char *v1, char *r); +static int w_math_log10(sip_msg_t *msg, char *v1, char *r); static int fixup_math_p2(void **param, int param_no); static int fixup_math_p3(void **param, int param_no); @@ -47,6 +49,10 @@ static cmd_export_t cmds[]={ 0, ANY_ROUTE}, {"math_log", (cmd_function)w_math_log, 2, fixup_math_p2, 0, ANY_ROUTE}, + {"math_log2", (cmd_function)w_math_log2, 2, fixup_math_p2, + 0, ANY_ROUTE}, + {"math_log10", (cmd_function)w_math_log10, 2, fixup_math_p2, + 0, ANY_ROUTE}, {0, 0, 0, 0, 0, 0} }; @@ -127,6 +133,62 @@ static int w_math_log(sip_msg_t *msg, char *v1, char *r) return 1; } +/** + * + */ +static int w_math_log2(sip_msg_t *msg, char *v1, char *r) +{ + int vi1 = 0; + pv_spec_t *dst; + pv_value_t val = {0}; + + if(fixup_get_ivalue(msg, (gparam_t*)v1, &vi1)<0) { + LM_ERR("failed to get first parameter value\n"); + return -1; + } + + dst = (pv_spec_t *)r; + if(dst->setf==NULL) { + LM_ERR("target pv is not writable\n"); + return -1; + } + + val.ri = (long)log2((double)vi1); + val.flags = PV_TYPE_INT|PV_VAL_INT; + + dst->setf(msg, &dst->pvp, (int)EQ_T, &val); + + return 1; +} + +/** + * + */ +static int w_math_log10(sip_msg_t *msg, char *v1, char *r) +{ + int vi1 = 0; + pv_spec_t *dst; + pv_value_t val = {0}; + + if(fixup_get_ivalue(msg, (gparam_t*)v1, &vi1)<0) { + LM_ERR("failed to get first parameter value\n"); + return -1; + } + + dst = (pv_spec_t *)r; + if(dst->setf==NULL) { + LM_ERR("target pv is not writable\n"); + return -1; + } + + val.ri = (long)log10((double)vi1); + val.flags = PV_TYPE_INT|PV_VAL_INT; + + dst->setf(msg, &dst->pvp, (int)EQ_T, &val); + + return 1; +} + /** * */