From 388a12b455da982c590472741b1007ef42cc8b91 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Fri, 14 Apr 2023 22:55:28 +0200 Subject: [PATCH] math: added math_sqrt() function --- src/modules/math/math_mod.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/modules/math/math_mod.c b/src/modules/math/math_mod.c index ae73a7bbc42..2ac7a977893 100644 --- a/src/modules/math/math_mod.c +++ b/src/modules/math/math_mod.c @@ -39,6 +39,7 @@ static int w_math_pow(sip_msg_t *msg, char *v1, char *v2, char *r); static int w_math_logN(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 w_math_sqrt(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); @@ -53,6 +54,8 @@ static cmd_export_t cmds[]={ 0, ANY_ROUTE}, {"math_log10", (cmd_function)w_math_log10, 2, fixup_math_p2, 0, ANY_ROUTE}, + {"math_sqrt", (cmd_function)w_math_sqrt, 2, fixup_math_p2, + 0, ANY_ROUTE}, {0, 0, 0, 0, 0, 0} }; @@ -189,6 +192,34 @@ static int w_math_log10(sip_msg_t *msg, char *v1, char *r) return 1; } +/** + * + */ +static int w_math_sqrt(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)sqrt((double)vi1); + val.flags = PV_TYPE_INT|PV_VAL_INT; + + dst->setf(msg, &dst->pvp, (int)EQ_T, &val); + + return 1; +} + /** * */