[sr-dev] git:master: app_lua: Added sr.xavp. get function in order to get a table with all the values of a xavp.

Daniel-Constantin Mierla miconda at gmail.com
Thu Apr 25 10:09:46 CEST 2013


Module: sip-router
Branch: master
Commit: c923dec79e20b77f0b42f0ef286eb396bb06f29c
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=c923dec79e20b77f0b42f0ef286eb396bb06f29c

Author: Victor Seva <linuxmaniac at torreviejawireless.org>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Mon Apr 22 17:09:19 2013 +0200

app_lua: Added sr.xavp.get function in order to get a table with all the values of a xavp.

---

 modules/app_lua/app_lua_sr.c |   92 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 92 insertions(+), 0 deletions(-)

diff --git a/modules/app_lua/app_lua_sr.c b/modules/app_lua/app_lua_sr.c
index 6b9dae6..4011e52 100644
--- a/modules/app_lua/app_lua_sr.c
+++ b/modules/app_lua/app_lua_sr.c
@@ -1144,6 +1144,97 @@ static int lua_sr_push_str_list_table(lua_State *L, struct str_list *list) {
 }
 
 /**
+ * creates and push a table to the lua stack with
+ * the elements of the xavp
+ */
+static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp) {
+	lua_Number i = 1;
+	sr_xavp_t *avp = NULL;
+
+	if(xavp->val.type!=SR_XTYPE_XAVP){
+		LM_ERR("%s not xavp?\n", xavp->name.s);
+		return 0;
+	}
+	avp = xavp->val.v.xavp;
+
+	lua_newtable(L);
+	while(avp!=NULL){
+		switch(avp->val.type) {
+			case SR_XTYPE_NULL:
+				lua_pushnil(L);
+				lua_setfield(L, -2, avp->name.s);
+			break;
+			case SR_XTYPE_INT:
+				i = avp->val.v.i;
+				lua_pushnumber(L, i);
+				lua_setfield(L, -2, avp->name.s);
+			break;
+			case SR_XTYPE_STR:
+				lua_pushlstring(L, avp->val.v.s.s, avp->val.v.s.len);
+				lua_setfield(L, -2, avp->name.s);
+			break;
+			case SR_XTYPE_TIME:
+			case SR_XTYPE_LONG:
+			case SR_XTYPE_LLONG:
+			case SR_XTYPE_DATA:
+				lua_pushnil(L);
+				lua_setfield(L, -2, avp->name.s);
+				LM_WARN("XAVP type:%d value not supported\n", avp->val.type);
+			break;
+			case SR_XTYPE_XAVP:
+				if(!lua_sr_push_xavp_table(L,avp->val.v.xavp)){
+					LM_ERR("xavp:%.*s subtable error. Nil value added\n", avp->name.len, avp->name.s);
+					lua_pushnil(L);
+				}
+				lua_setfield(L, -2, avp->name.s);
+			break;
+		}
+		avp = avp->next;
+	}
+	return 1;
+}
+
+/**
+ * puts a table with content of a xavp
+ */
+static int lua_sr_xavp_get(lua_State *L)
+{
+	str xavp_name;
+	int indx = 0;
+	sr_lua_env_t *env_L;
+	sr_xavp_t *avp;
+
+	env_L = sr_lua_env_get();
+
+	if(lua_gettop(L)<2)
+	{
+		LM_ERR("to few parameters [%d]\n",lua_gettop(L));
+		return 0;
+	}
+
+	if(!lua_isnumber(L, -1))
+	{
+		LM_ERR("invalid int parameter\n");
+		return 0;
+	}
+	indx = lua_tointeger(L, -1);
+
+	xavp_name.s = (char*)lua_tostring(L, -2);
+	if(xavp_name.s==NULL || env_L->msg==NULL)
+		return 0;
+	xavp_name.len = strlen(xavp_name.s);
+
+	avp = xavp_get_by_index(&xavp_name, indx, NULL);
+	if(avp==NULL){
+		LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
+		lua_pushnil(L);
+		return 1;
+	}
+	lua_sr_push_xavp_table(L, avp);
+	return 1;
+}
+
+/**
  * puts a table with the list of keys of the xavp
  */
 static int lua_sr_xavp_get_keys (lua_State *L)
@@ -1195,6 +1286,7 @@ static int lua_sr_xavp_get_keys (lua_State *L)
  *
  */
 static const luaL_reg _sr_xavp_Map [] = {
+	{"get", lua_sr_xavp_get},
 	{"get_keys",  lua_sr_xavp_get_keys},
 	{NULL, NULL}
 };




More information about the sr-dev mailing list