From 63a7d5c4f0b21d65f5676202fd3b66fe264aa76c Mon Sep 17 00:00:00 2001 From: Eldon Schoop Date: Fri, 27 Feb 2015 23:37:49 -0800 Subject: [PATCH 1/4] First implementation of svcd_subscribe --- natlib/svcd.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/natlib/svcd.c b/natlib/svcd.c index b188a5c..fd74ad3 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -185,3 +185,59 @@ static int svcd_init( lua_State *L ) return 0; } + +static int svcd_subscribe(lua_state* L) +{ + /* + local msg = storm.array.create(7,storm.array.UINT8) + local ivkid = SVCD.ivkid + SVCD.ivkid = SVCD.ivkid + 1 + if SVCD.ivkid > 65535 then + SVCD.ivkid = 0 + end + SVCD.oursubs[ivkid] = on_notify + msg:set(1, 1) + msg:set_as(storm.array.UINT16, 1, svcid) + msg:set_as(storm.array.UINT16, 3, attrid) + msg:set_as(storm.array.UINT16, 5, ivkid) + storm.net.sendto(SVCD.ncsock, msg:as_str(), targetip, 2530) + return ivkid + */ + if (lua_gettop(L) != 4) return luaL_error(L, "Expected (targetip, svcid, attrid, on_notify)"); + + lua_getglobal(L, "SVCD"); + lua_getfield(L, -1, "ivkid"); + + int16 ivkid = checknumber(...); + lua_pop(L, 1); + int16 new_ivkid = 0 ? invkid > 65535 : invkid + 1; + lua_pushnumber(L, new_ivkid); + lua_setfield(L, -1, "ivkid"); + + lua_getfield(L, -1, "oursubs"); + // put value on the stack + lua_pushvalue(L, 4); // on_notify + lua_seti(L, -2, ivkid); + lua_pop(L, 2); + + + lua_pushlightfunction(L, storm_net_sendto); + + lua_getglobal(L, "SVCD"); + lua_getfield(L, -1, "ncsock"); + lua_remove(L, -2); // remove SVCD + + + uint8_t msg[7]; + msg[0] = 1; + ((uint16_t*) (msg+1))[0] = svcid; + ((uint16_t*) (msg+1))[1] = attrid; + ((uint16_t*) (msg+1))[2] = ivkid; + lua_pushlstring(L, msg, 7); + + lua_pushvalue(L, 1); + lua_pushnumber(L, 2530); + lua_call(L, 4, 0); + lua_pushnumber(L, ivkid); + return 0; +} From be8788498c7da0d3eeadb9459fdbd91927defe64 Mon Sep 17 00:00:00 2001 From: Eldon Schoop Date: Sat, 28 Feb 2015 00:17:02 -0800 Subject: [PATCH 2/4] svcd subscribe --- natlib/svcd.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/natlib/svcd.c b/natlib/svcd.c index fd74ad3..9c84cb7 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -2,7 +2,8 @@ #define SVCD_SYMBOLS \ - { LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) }, + { LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) }, \ + { LSTRKEY( "svcd_subscribe"), LFUNCVAL ( svcd_subscribe ) }, //If this file is defining only specific functions, or if it @@ -186,42 +187,33 @@ static int svcd_init( lua_State *L ) return 0; } -static int svcd_subscribe(lua_state* L) +static int svcd_subscribe(lua_State* L) { - /* - local msg = storm.array.create(7,storm.array.UINT8) - local ivkid = SVCD.ivkid - SVCD.ivkid = SVCD.ivkid + 1 - if SVCD.ivkid > 65535 then - SVCD.ivkid = 0 - end - SVCD.oursubs[ivkid] = on_notify - msg:set(1, 1) - msg:set_as(storm.array.UINT16, 1, svcid) - msg:set_as(storm.array.UINT16, 3, attrid) - msg:set_as(storm.array.UINT16, 5, ivkid) - storm.net.sendto(SVCD.ncsock, msg:as_str(), targetip, 2530) - return ivkid - */ if (lua_gettop(L) != 4) return luaL_error(L, "Expected (targetip, svcid, attrid, on_notify)"); lua_getglobal(L, "SVCD"); lua_getfield(L, -1, "ivkid"); - int16 ivkid = checknumber(...); + uint16_t ivkid = luaL_checkinteger(L, -1); lua_pop(L, 1); - int16 new_ivkid = 0 ? invkid > 65535 : invkid + 1; + + uint16_t new_ivkid = ivkid + 1; + if (new_ivkid > 65535) { + new_ivkid = 0; + } + lua_pushnumber(L, new_ivkid); lua_setfield(L, -1, "ivkid"); lua_getfield(L, -1, "oursubs"); // put value on the stack + lua_pushinteger(L, ivkid); lua_pushvalue(L, 4); // on_notify - lua_seti(L, -2, ivkid); + lua_settable(L, -3); lua_pop(L, 2); - lua_pushlightfunction(L, storm_net_sendto); + lua_pushlightfunction(L, libstorm_net_sendto); lua_getglobal(L, "SVCD"); lua_getfield(L, -1, "ncsock"); @@ -230,8 +222,8 @@ static int svcd_subscribe(lua_state* L) uint8_t msg[7]; msg[0] = 1; - ((uint16_t*) (msg+1))[0] = svcid; - ((uint16_t*) (msg+1))[1] = attrid; + ((uint16_t*) (msg+1))[0] = luaL_checknumber(L, 2); //svcid + ((uint16_t*) (msg+1))[1] = luaL_checknumber(L, 3); //attrid ((uint16_t*) (msg+1))[2] = ivkid; lua_pushlstring(L, msg, 7); From f95ab295fb631951645a8ed45cfeb77a2c6c9125 Mon Sep 17 00:00:00 2001 From: Nikita Kitaev Date: Sun, 1 Mar 2015 23:57:30 -0800 Subject: [PATCH 3/4] Cleanup whitespace in subscribe --- natlib/svcd.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/natlib/svcd.c b/natlib/svcd.c index 9c84cb7..3a18705 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -190,10 +190,10 @@ static int svcd_init( lua_State *L ) static int svcd_subscribe(lua_State* L) { if (lua_gettop(L) != 4) return luaL_error(L, "Expected (targetip, svcid, attrid, on_notify)"); - + lua_getglobal(L, "SVCD"); lua_getfield(L, -1, "ivkid"); - + uint16_t ivkid = luaL_checkinteger(L, -1); lua_pop(L, 1); @@ -204,29 +204,29 @@ static int svcd_subscribe(lua_State* L) lua_pushnumber(L, new_ivkid); lua_setfield(L, -1, "ivkid"); - + lua_getfield(L, -1, "oursubs"); // put value on the stack lua_pushinteger(L, ivkid); lua_pushvalue(L, 4); // on_notify lua_settable(L, -3); lua_pop(L, 2); - - + + lua_pushlightfunction(L, libstorm_net_sendto); - + lua_getglobal(L, "SVCD"); lua_getfield(L, -1, "ncsock"); lua_remove(L, -2); // remove SVCD - - + + uint8_t msg[7]; msg[0] = 1; ((uint16_t*) (msg+1))[0] = luaL_checknumber(L, 2); //svcid ((uint16_t*) (msg+1))[1] = luaL_checknumber(L, 3); //attrid ((uint16_t*) (msg+1))[2] = ivkid; lua_pushlstring(L, msg, 7); - + lua_pushvalue(L, 1); lua_pushnumber(L, 2530); lua_call(L, 4, 0); From d6809ed075e178d82f82893b8a9edc871329b15c Mon Sep 17 00:00:00 2001 From: Nikita Kitaev Date: Mon, 2 Mar 2015 00:21:01 -0800 Subject: [PATCH 4/4] Fixes to subscribe --- natlib/svcd.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/natlib/svcd.c b/natlib/svcd.c index 3a18705..8c5d2ff 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -191,11 +191,15 @@ static int svcd_subscribe(lua_State* L) { if (lua_gettop(L) != 4) return luaL_error(L, "Expected (targetip, svcid, attrid, on_notify)"); - lua_getglobal(L, "SVCD"); - lua_getfield(L, -1, "ivkid"); + // arg 1 is targetip, to be passed along to to libstorm_net_sendto + uint16_t svcid = luaL_checknumber(L, 2); + uint16_t attrid = luaL_checknumber(L, 3); + // arg 4 is on_notify + + lua_getglobal(L, "SVCD"); // position 5 on stack + lua_getfield(L, 5, "ivkid"); - uint16_t ivkid = luaL_checkinteger(L, -1); - lua_pop(L, 1); + uint16_t ivkid = lua_tointeger(L, -1); uint16_t new_ivkid = ivkid + 1; if (new_ivkid > 65535) { @@ -203,33 +207,31 @@ static int svcd_subscribe(lua_State* L) } lua_pushnumber(L, new_ivkid); - lua_setfield(L, -1, "ivkid"); + lua_setfield(L, 5, "ivkid"); - lua_getfield(L, -1, "oursubs"); + lua_getfield(L, 5, "oursubs"); // put value on the stack lua_pushinteger(L, ivkid); lua_pushvalue(L, 4); // on_notify lua_settable(L, -3); - lua_pop(L, 2); - lua_pushlightfunction(L, libstorm_net_sendto); - lua_getglobal(L, "SVCD"); - lua_getfield(L, -1, "ncsock"); - lua_remove(L, -2); // remove SVCD + lua_getfield(L, 5, "ncsock"); + char msg[8] __attribute__((aligned(2))); - uint8_t msg[7]; - msg[0] = 1; - ((uint16_t*) (msg+1))[0] = luaL_checknumber(L, 2); //svcid - ((uint16_t*) (msg+1))[1] = luaL_checknumber(L, 3); //attrid - ((uint16_t*) (msg+1))[2] = ivkid; - lua_pushlstring(L, msg, 7); + // don't use the msg[0] byte + msg[1] = 1; + ((uint16_t*) msg)[1] = svcid; + ((uint16_t*) msg)[2] = attrid; + ((uint16_t*) msg)[3] = ivkid; + lua_pushlstring(L, &(msg[1]), 7); - lua_pushvalue(L, 1); + lua_pushvalue(L, 1); // targetip lua_pushnumber(L, 2530); lua_call(L, 4, 0); + lua_pushnumber(L, ivkid); - return 0; + return 1; }