[sr-dev] git:master:1576dc7c: app_lua: new KEMI pv functions KSR.pv.getvn(...) and KSR.pv.getvs(...)

Daniel-Constantin Mierla miconda at gmail.com
Fri Aug 24 11:46:03 CEST 2018


Module: kamailio
Branch: master
Commit: 1576dc7cf504a3a30e83919222f1b8441e4207b5
URL: https://github.com/kamailio/kamailio/commit/1576dc7cf504a3a30e83919222f1b8441e4207b5

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2018-08-24T11:43:53+02:00

app_lua: new KEMI pv functions KSR.pv.getvn(...) and KSR.pv.getvs(...)

- KSR.pv.getvn(pv, ival) - return the value of pv if not null,
 otherwise return the int value ival
- KSR.pv.getvs(pv, sval) - return the value of pv if not null,
 otherwise return the str value sval

---

Modified: src/modules/app_lua/app_lua_sr.c

---

Diff:  https://github.com/kamailio/kamailio/commit/1576dc7cf504a3a30e83919222f1b8441e4207b5.diff
Patch: https://github.com/kamailio/kamailio/commit/1576dc7cf504a3a30e83919222f1b8441e4207b5.patch

---

diff --git a/src/modules/app_lua/app_lua_sr.c b/src/modules/app_lua/app_lua_sr.c
index a57f94fcc7..61c661852d 100644
--- a/src/modules/app_lua/app_lua_sr.c
+++ b/src/modules/app_lua/app_lua_sr.c
@@ -901,6 +901,19 @@ static int lua_sr_pv_push_val_null (lua_State *L, int rmode)
 	}
 }
 
+/**
+ *
+ */
+static int lua_sr_pv_push_valx (lua_State *L, int rmode, int vi, str *vs)
+{
+	if(rmode==1) {
+		lua_pushlstring(L, vs->s, vs->len);
+	} else {
+		lua_pushinteger(L, vi);
+	}
+	return 1;
+}
+
 /**
  *
  */
@@ -971,6 +984,89 @@ static int lua_sr_pv_gete (lua_State *L)
 	return lua_sr_pv_get_val(L, 2);
 }
 
+/**
+ *
+ */
+static int lua_sr_pv_get_valx (lua_State *L, int rmode)
+{
+	str pvn;
+	pv_spec_t *pvs;
+	pv_value_t val;
+	sr_lua_env_t *env_L;
+	int pl;
+	int xival = 0;
+	str xsval = str_init("");
+
+	env_L = sr_lua_env_get();
+
+	if(lua_gettop(L)<2) {
+		LM_ERR("to few parameters [%d]\n", lua_gettop(L));
+		return lua_sr_pv_push_val_null(L, 0);
+	}
+	if(rmode==1) {
+		if(!lua_isnumber(L, -1)) {
+			LM_ERR("invalid int parameter\n");
+			return lua_sr_pv_push_val_null(L, 0);
+		}
+		xival = lua_tointeger(L, -1);
+	} else {
+		if(!lua_isstring(L, -1)) {
+			LM_ERR("invalid str parameter\n");
+			return lua_sr_pv_push_val_null(L, 0);
+		}
+		xsval.s = (char*)lua_tostring(L, -1);
+		xsval.len = strlen(xsval.s);
+	}
+
+	pvn.s = (char*)lua_tostring(L, -2);
+	if(pvn.s==NULL || env_L->msg==NULL)
+		return lua_sr_pv_push_valx(L, rmode, xival, &xsval);
+
+	pvn.len = strlen(pvn.s);
+	LM_DBG("pv set: %s\n", pvn.s);
+	pl = pv_locate_name(&pvn);
+	if(pl != pvn.len) {
+		LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
+		return lua_sr_pv_push_valx(L, rmode, xival, &xsval);
+	}
+	pvs = pv_cache_get(&pvn);
+	if(pvs==NULL) {
+		LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
+		return lua_sr_pv_push_valx(L, rmode, xival, &xsval);
+	}
+
+	memset(&val, 0, sizeof(pv_value_t));
+	if(pv_get_spec_value(env_L->msg, pvs, &val) != 0) {
+		LM_ERR("unable to get pv value for [%s]\n", pvn.s);
+		return lua_sr_pv_push_valx(L, rmode, xival, &xsval);
+	}
+	if(val.flags&PV_VAL_NULL) {
+		return lua_sr_pv_push_valx(L, rmode, xival, &xsval);
+	}
+	if(val.flags&PV_TYPE_INT) {
+		lua_pushinteger(L, val.ri);
+		return 1;
+	}
+	lua_pushlstring(L, val.rs.s, val.rs.len);
+	return 1;
+}
+
+/**
+ *
+ */
+static int lua_sr_pv_getvs (lua_State *L)
+{
+	return lua_sr_pv_get_valx(L, 0);
+}
+
+/**
+ *
+ */
+static int lua_sr_pv_getvn (lua_State *L)
+{
+	return lua_sr_pv_get_valx(L, 1);
+}
+
 /**
  *
  */
@@ -1179,6 +1275,8 @@ static const luaL_Reg _sr_pv_Map [] = {
 	{"get",      lua_sr_pv_get},
 	{"getw",     lua_sr_pv_getw},
 	{"gete",     lua_sr_pv_gete},
+	{"getvn",    lua_sr_pv_getvn},
+	{"getvs",    lua_sr_pv_getvs},
 	{"seti",     lua_sr_pv_seti},
 	{"sets",     lua_sr_pv_sets},
 	{"unset",    lua_sr_pv_unset},




More information about the sr-dev mailing list