Module: kamailio
Branch: master
Commit: 17d8a04033a19d6781f8f14c9ae610cd070c2a47
URL:
https://github.com/kamailio/kamailio/commit/17d8a04033a19d6781f8f14c9ae610c…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2023-04-14T11:53:02+02:00
math: additional logarithm functions
---
Modified: src/modules/math/doc/math_admin.xml
Modified: src/modules/math/math_mod.c
---
Diff:
https://github.com/kamailio/kamailio/commit/17d8a04033a19d6781f8f14c9ae610c…
Patch:
https://github.com/kamailio/kamailio/commit/17d8a04033a19d6781f8f14c9ae610c…
---
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 @@
</programlisting>
</example>
</section>
-<section id="math.f.log">
+ <section id="math.f.log">
<title>
<function moreinfo="none">log(x, res)</function>
</title>
@@ -97,7 +97,51 @@
...
log("10", "$var(res)");
$var(x) = 10;
- pow("$var(x)", "$var(res)");
+ log("$var(x)", "$var(res)");
+...
+</programlisting>
+ </example>
+ </section>
+ <section id="math.f.log2">
+ <title>
+ <function moreinfo="none">log2(x, res)</function>
+ </title>
+ <para>
+ The function computes the base-2 logarithm of x and stores the result
+ in res.
+ </para>
+ <para>
+ This function can be used from ANY_ROUTE.
+ </para>
+ <example>
+ <title><function>log2</function> usage</title>
+ <programlisting format="linespecific">
+...
+ log2("16", "$var(res)");
+ $var(x) = 16;
+ log2("$var(x)", "$var(res)");
+...
+</programlisting>
+ </example>
+ </section>
+ <section id="math.f.log10">
+ <title>
+ <function moreinfo="none">log10(x, res)</function>
+ </title>
+ <para>
+ The function computes the base-10 logarithm of x and stores the result
+ in res.
+ </para>
+ <para>
+ This function can be used from ANY_ROUTE.
+ </para>
+ <example>
+ <title><function>log10</function> usage</title>
+ <programlisting format="linespecific">
+...
+ log10("100", "$var(res)");
+ $var(x) = 100;
+ log10("$var(x)", "$var(res)");
...
</programlisting>
</example>
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;
+}
+
/**
*
*/