From fe438a790b6de651720231d53d333d3cae881bb8 Mon Sep 17 00:00:00 2001 From: mschuldt Date: Wed, 25 Feb 2015 15:19:25 -0800 Subject: [PATCH 1/5] initial port to c (untested) --- natlib/svcd.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 natlib/svcd.c diff --git a/natlib/svcd.c b/natlib/svcd.c new file mode 100644 index 0000000..4e1ef25 --- /dev/null +++ b/natlib/svcd.c @@ -0,0 +1,75 @@ + +#include "libstormarray.h" +#include "lrodefs.h" + +int SVCD_subdispatch(lua_State L){ + //local parr = storm.array.fromstr(pay) + arr_from_str(L); + storm_array_t *parr = lua_touserdata(L, -1); + int srcip = lua_tointeger(L, 2); //? what type is the ip? + int strcport = lua_tointeger(L, 3); + + //local cmd = parr:get(1); + char cmd = array_get(parr, 1); + //local svc_id = parr:get_as(storm.array.UINT16,1); + int svc_id = array_get_as(parr, ARR_TYPE_UINT16, 1); + //local attr_id = parr:get_as(storm.array.UINT16,3); + int attr_id = array_get_as(parr, ARR_TYPE_UINT16 3); + //local ivkid = parr:get_as(storm.array.UINT16, 5); + int ivkid = array_get_as(parr, ARR_TYPE_UINT16, 5); + + lua_getglobal(L, "SVCD"); + lua_pushstring(L, "subscribers"); + lua_gettable(L, -1); + lua_pushinteger(L, svc_id); + lua_gettable(L, -1); + //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id] + if (cmd == 1){ //subscribe command + if (lua_isnil(L,-1)){ // if (SVCD.subscribers[svc_id] == nil){ + lua_pop(L, 1); //stack: SVCD, SVCD[subscribers] + //SVCD.subscribers[svc_id] = {}; + lua_newtable(L); + lua_pushinteger(L, svc_id); + lua_pushvalue(L, -2); + //stack: SVCD, SVCD[subscribers], , svc_id, + lua_settable(L, -4); + } + //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id], + //if (SVCD.subscribers[svc_id][attr_id] == nil){ + lua_pushstring("attr_id"); + lua_gettable(L, -2); + if (lua_isnil(L, -1)){ + //SVCD.subscribers[svc_id][attr_id] = {} + lua_pop(L, 1); + lua_newtable(L); + lua_pushinteger(L, attr_id); + lua_pushvalue(L, -2); + lua_settable(L, -4); + } + //stack: SVCD[subscribers], SVCD[subscribers][svc_id], SVCD[subscribers][svc_id][attr_id] + + //SVCD.subscribers[svc_id][attr_id][srcip] = ivkid; + lua_pushnumber(L, srcip); + lua_pushnumber(L, ivkid); + lua_settable(L, -3); + + }else if (cmd == 2){ //unsubscribe command + //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id] + + //if (SVCD.subscribers[svc_id] == nil){ + if (lua_isnil(L, -1)){ + return; + } + //if (SVCD.subscribers[svc_id][attr_id] == nil){ + lua_pushnumber(L, attr_id); + lua_gettable(L, -2); + if (lua_isnil(L, -1)){ + return; + } + //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id], SVCD[subscribers][svc_id][attr_id] + //SVCD.subscribers[svc_id][attr_id][srcip] = nil; + lua_pushnumber(L, srcip); + lua_pushnil(L); + lau_settable(L, -2); + } +} From c3fd8f278c7497f41419c26a9d1d85655907d275 Mon Sep 17 00:00:00 2001 From: mschuldt Date: Thu, 26 Feb 2015 00:14:21 -0800 Subject: [PATCH 2/5] convert to c string instead of storm array --- natlib/svcd.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/natlib/svcd.c b/natlib/svcd.c index 4e1ef25..810fe91 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -4,19 +4,18 @@ int SVCD_subdispatch(lua_State L){ //local parr = storm.array.fromstr(pay) - arr_from_str(L); - storm_array_t *parr = lua_touserdata(L, -1); + char *parr = lua_tostring(L, 1); int srcip = lua_tointeger(L, 2); //? what type is the ip? int strcport = lua_tointeger(L, 3); //local cmd = parr:get(1); - char cmd = array_get(parr, 1); + char cmd = parr[0]; //local svc_id = parr:get_as(storm.array.UINT16,1); - int svc_id = array_get_as(parr, ARR_TYPE_UINT16, 1); + int svc_id = ((int16_t*)parr)[1]; //local attr_id = parr:get_as(storm.array.UINT16,3); - int attr_id = array_get_as(parr, ARR_TYPE_UINT16 3); + int attr_id = ((int16_t*)parr)[3]; //local ivkid = parr:get_as(storm.array.UINT16, 5); - int ivkid = array_get_as(parr, ARR_TYPE_UINT16, 5); + int ivkid = ((int16_t*)parr)[5]; lua_getglobal(L, "SVCD"); lua_pushstring(L, "subscribers"); From 746d16f24b338cbf5840651793e6074d23e0aea3 Mon Sep 17 00:00:00 2001 From: Kavan Sikand Date: Fri, 27 Feb 2015 14:15:35 -0800 Subject: [PATCH 3/5] fixed subdispatch bugs, runs now --- app/svcdTest.lua | 16 ++++++++++++++++ lib/svcd/svcd.lua | 25 ++++++++++++++++++++++++- native.c | 2 ++ natlib/svcd.c | 33 ++++++++++++++++++--------------- 4 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 app/svcdTest.lua diff --git a/app/svcdTest.lua b/app/svcdTest.lua new file mode 100644 index 0000000..4a54f5f --- /dev/null +++ b/app/svcdTest.lua @@ -0,0 +1,16 @@ +require "cord" +sh = require "stormsh" + +-- start a coroutine that provides a REPL +sh.start() + +require "svcd" +require "pprint" + +SVCD.init("kavan", function() print("ready to go") end) +SVCD.add_service(12) +SVCD.add_attribute(12,1,function() print("someone wrote to attribute") end) + +-- enter the main event loop. This puts the processor to sleep +-- in between events +cord.enter_loop() diff --git a/lib/svcd/svcd.lua b/lib/svcd/svcd.lua index d2d9a6f..d03e76a 100644 --- a/lib/svcd/svcd.lua +++ b/lib/svcd/svcd.lua @@ -82,7 +82,7 @@ SVCD.init = function(id, onready) -- notification client socket SVCD.ncsock = storm.net.udpsocket(2529, SVCD.ncdispatch) -- subscription socket - SVCD.subsock = storm.net.udpsocket(2530, SVCD.subdispatch) + SVCD.subsock = storm.net.udpsocket(2530, storm.n.subdispatch) SVCD.manifest = {id=id } if id ~= nil then storm.os.invokePeriodically(3*storm.os.SECOND, function() @@ -132,8 +132,31 @@ SVCD.add_attribute = function(svc_id, attr_id, write_fn) SVCD.manifest_map[svc_id][attr_id] = write_fn end +SVCD.test_subdispatch = function() + SVCD.subscribers = {} + local msg = storm.array.create(7,storm.array.UINT8) + 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) + SVCD.subdispatch(msg,"ff01::1",2343) + outputTable = SVCD.subscribers + + SVCD.subscribers = {} + storm.n.subdispatch(msg,"ff01::1",2343) + nativeOutputTable = SVCD.subscribers + + require "pprint" + print("\n lua:\n") + ppNestedTable(outputTable) + print("\n native:\n") + ppNestedTable(nativeOutputTable) +end + SVCD.subdispatch = function(pay, srcip, srcport) local parr = storm.array.fromstr(pay) + print("array"); + print(parr); local cmd = parr:get(1) local svc_id = parr:get_as(storm.array.UINT16,1) local attr_id = parr:get_as(storm.array.UINT16,3) diff --git a/native.c b/native.c index 03bd941..4471d3a 100644 --- a/native.c +++ b/native.c @@ -25,6 +25,7 @@ //Include some libs as C files into this file #include "natlib/util.c" +#include "natlib/svcd.c" ////////////////// BEGIN FUNCTIONS ///////////////////////////// @@ -176,6 +177,7 @@ const LUA_REG_TYPE contrib_native_map[] = { LSTRKEY( "fourth_root"), LFUNCVAL ( contrib_fourth_root_m1000 ) }, { LSTRKEY( "run_foobar"), LFUNCVAL ( contrib_run_foobar ) }, { LSTRKEY( "makecounter"), LFUNCVAL ( contrib_makecounter ) }, + { LSTRKEY( "subdispatch"), LFUNCVAL ( SVCD_subdispatch ) }, /* Constants for the Temp sensor. */ // -- Register address -- diff --git a/natlib/svcd.c b/natlib/svcd.c index 810fe91..bfb0d5e 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -2,26 +2,27 @@ #include "libstormarray.h" #include "lrodefs.h" -int SVCD_subdispatch(lua_State L){ +int SVCD_subdispatch(lua_State *L){ //local parr = storm.array.fromstr(pay) - char *parr = lua_tostring(L, 1); - int srcip = lua_tointeger(L, 2); //? what type is the ip? + size_t len; + char *parr = lua_tolstring(L, 1, &len); + char* srcip = lua_tostring(L, 2); //? what type is the ip? int strcport = lua_tointeger(L, 3); //local cmd = parr:get(1); char cmd = parr[0]; //local svc_id = parr:get_as(storm.array.UINT16,1); - int svc_id = ((int16_t*)parr)[1]; + int16_t svc_id = *(int16_t*)(parr+1); //local attr_id = parr:get_as(storm.array.UINT16,3); - int attr_id = ((int16_t*)parr)[3]; + int16_t attr_id = *(int16_t*)(parr+3); //local ivkid = parr:get_as(storm.array.UINT16, 5); - int ivkid = ((int16_t*)parr)[5]; + int16_t ivkid = *(int16_t*)(parr+5); lua_getglobal(L, "SVCD"); lua_pushstring(L, "subscribers"); - lua_gettable(L, -1); + lua_gettable(L, -2); lua_pushinteger(L, svc_id); - lua_gettable(L, -1); + lua_gettable(L, -2); //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id] if (cmd == 1){ //subscribe command if (lua_isnil(L,-1)){ // if (SVCD.subscribers[svc_id] == nil){ @@ -35,7 +36,7 @@ int SVCD_subdispatch(lua_State L){ } //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id], //if (SVCD.subscribers[svc_id][attr_id] == nil){ - lua_pushstring("attr_id"); + lua_pushstring(L,"attr_id"); lua_gettable(L, -2); if (lua_isnil(L, -1)){ //SVCD.subscribers[svc_id][attr_id] = {} @@ -48,27 +49,29 @@ int SVCD_subdispatch(lua_State L){ //stack: SVCD[subscribers], SVCD[subscribers][svc_id], SVCD[subscribers][svc_id][attr_id] //SVCD.subscribers[svc_id][attr_id][srcip] = ivkid; - lua_pushnumber(L, srcip); + lua_pushstring(L, srcip); lua_pushnumber(L, ivkid); lua_settable(L, -3); - }else if (cmd == 2){ //unsubscribe command + }else if (cmd == 0){ //unsubscribe command //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id] //if (SVCD.subscribers[svc_id] == nil){ if (lua_isnil(L, -1)){ - return; + return 0; } //if (SVCD.subscribers[svc_id][attr_id] == nil){ lua_pushnumber(L, attr_id); lua_gettable(L, -2); if (lua_isnil(L, -1)){ - return; + return 0; } //stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id], SVCD[subscribers][svc_id][attr_id] //SVCD.subscribers[svc_id][attr_id][srcip] = nil; - lua_pushnumber(L, srcip); + lua_pushstring(L, srcip); lua_pushnil(L); - lau_settable(L, -2); + lua_settable(L, -3); } + + return 0; } From e2116497e27893ad4815cee28433be02de5a8dd1 Mon Sep 17 00:00:00 2001 From: Kavan Sikand Date: Fri, 27 Feb 2015 15:03:13 -0800 Subject: [PATCH 4/5] usign native C approach, ready to go --- app/svcdTest.lua | 6 +++--- lib/svcd/svcd.lua | 2 -- natlib/svcd.c | 14 +++++--------- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/svcdTest.lua b/app/svcdTest.lua index 4a54f5f..4de881d 100644 --- a/app/svcdTest.lua +++ b/app/svcdTest.lua @@ -7,9 +7,9 @@ sh.start() require "svcd" require "pprint" -SVCD.init("kavan", function() print("ready to go") end) -SVCD.add_service(12) -SVCD.add_attribute(12,1,function() print("someone wrote to attribute") end) +storm.n.svcd_init("kavan", function() print("ready to go") end) +-- SVCD.add_service(12) +-- SVCD.add_attribute(12,1,function() print("someone wrote to attribute") end) -- enter the main event loop. This puts the processor to sleep -- in between events diff --git a/lib/svcd/svcd.lua b/lib/svcd/svcd.lua index 70ee996..d00448e 100644 --- a/lib/svcd/svcd.lua +++ b/lib/svcd/svcd.lua @@ -155,8 +155,6 @@ end SVCD.subdispatch = function(pay, srcip, srcport) local parr = storm.array.fromstr(pay) - print("array"); - print(parr); local cmd = parr:get(1) local svc_id = parr:get_as(storm.array.UINT16,1) local attr_id = parr:get_as(storm.array.UINT16,3) diff --git a/natlib/svcd.c b/natlib/svcd.c index 9db834f..fb44fc5 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -1,15 +1,13 @@ #include "libstormarray.h" #include "lrodefs.h" +static int svcd_subdispatch(lua_State *L); //This file is included into native.c - #define SVCD_SYMBOLS \ - { LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) }, - { LSTRKEY( "svcd_subdispatch"), LFUNCVAL ( SVCD_subdispatch ) }, - - + { LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) }, \ + { LSTRKEY( "svcd_subdispatch"), LFUNCVAL ( svcd_subdispatch ) }, //If this file is defining only specific functions, or if it //is defining the whole thing @@ -149,8 +147,7 @@ static int svcd_init( lua_State *L ) lua_pushstring(L, "subsock"); lua_pushlightfunction(L, libstorm_net_udpsocket); lua_pushnumber(L, 2530); - lua_pushstring(L, "subdispatch"); - lua_gettable(L, 3); + lua_pushlightfunction(L, svcd_subdispatch); lua_call(L, 2, 1); lua_settable(L, 3); //Store @@ -198,8 +195,7 @@ static int svcd_init( lua_State *L ) // Maintainer: Running with scissors ////////////////////////////////////////////////////////////////////////////// -int SVCD_subdispatch(lua_State *L){ - printf("running native\n"); +static int svcd_subdispatch(lua_State *L) { //local parr = storm.array.fromstr(pay) size_t len; char *parr = lua_tolstring(L, 1, &len); From 07a4476d8eca5ca90d7d0a75d23bc768bdc6166d Mon Sep 17 00:00:00 2001 From: mschuldt Date: Sun, 1 Mar 2015 10:53:25 -0800 Subject: [PATCH 5/5] fixes --- natlib/svcd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/natlib/svcd.c b/natlib/svcd.c index fb44fc5..751dd07 100644 --- a/natlib/svcd.c +++ b/natlib/svcd.c @@ -198,12 +198,12 @@ static int svcd_init( lua_State *L ) static int svcd_subdispatch(lua_State *L) { //local parr = storm.array.fromstr(pay) size_t len; - char *parr = lua_tolstring(L, 1, &len); - char* srcip = lua_tostring(L, 2); //? what type is the ip? - int strcport = lua_tointeger(L, 3); + uint8_t *parr __attribute__((aligned(2))) = lua_tolstring(L, 1, &len); + uint8_t *srcip = lua_tostring(L, 2); + uint32_t strcport = lua_tointeger(L, 3); //local cmd = parr:get(1); - char cmd = parr[0]; + uint8_t cmd = parr[0]; //local svc_id = parr:get_as(storm.array.UINT16,1); int16_t svc_id = *(int16_t*)(parr+1); //local attr_id = parr:get_as(storm.array.UINT16,3);