Skip to content

SVCD::write [NOT READY] #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/blink.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("storm") -- libraries for interfacing with the board and kernel
require("cord") -- scheduler / fiber library
shield = require("starter") -- interfaces for resources on starter shield
require("svcd")

print ("blink test ")

Expand All @@ -19,6 +20,12 @@ function blinker(color)
end

shield.LED.start() -- enable LEDs
storm.os.invokePeriodically(1*storm.os.SECOND, blinker("red"))
local red_light = blinker("red")
print(red_light)

SVCD.test(1, red_light)
SVCD.test(2, red_light)
SVCD.test(3, red_light)
SVCD.test(4, red_light)
cord.enter_loop() -- start event/sleep loop

7 changes: 6 additions & 1 deletion app/ledpanel/ledclient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sh.start()
require "svcd"

flashers = {}

cord.new(function()
cord.await(SVCD.init, "ledclient")
SVCD.advert_received = function(pay, srcip, srcport)
Expand Down Expand Up @@ -36,6 +36,9 @@ function flashem(lednum, times)
cmd:set(1,lednum)
cmd:set(2,times)
local stat = cord.await(SVCD.write, k, 0x3003, 0x4005, cmd:as_str(), 300)
-- body


if stat ~= SVCD.OK then
print "FAIL"
else
Expand All @@ -54,5 +57,7 @@ function get_motd(serial)
end)
end

-- SVCD.write("fe80::212:6d02:0:304d", 0x3003, 0x4005, "goats", 300);

cord.enter_loop()

61 changes: 46 additions & 15 deletions lib/svcd/svcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,52 @@ SVCD.wcdispatch = function(pay, srcip, srcport)
end
end

SVCD.write = function (targetip, svcid, attrid, payload, timeout_ms, on_done)
local ivkid = SVCD.ivkid
SVCD.ivkid = SVCD.ivkid + 1
if SVCD.ivkid > 65535 then
SVCD.ivkid = 0
end
SVCD.handlers[ivkid] = on_done
storm.os.invokeLater(timeout_ms*storm.os.MILLISECOND, function()
if SVCD.handlers[ivkid] ~= nil then
SVCD.handlers[ivkid](SVCD.TIMEOUT)
SVCD.handlers[ivkid] = nil
end
end)
storm.net.sendto(SVCD.wcsock, storm.mp.pack({svcid, attrid, ivkid, payload}), targetip, 2526)
end
-- SVCD.test = function(delay, ondone)
-- print("Testing", delay * storm.os.SECOND, ondone)
-- storm.os.invokeLater(delay * storm.os.SECOND, ondone)
-- end

-- Sexy write 1 375000 20008fc0
-- Sexy write 2 750000 20008fc0
-- Sexy write 3 1125000 20008fc0
-- Sexy write 4 1500000 20008fc0

SVCD.test = storm.n.svcd_test
--
SVCD.write = storm.n.svcd_write

-- SVCD.write = function (targetip, svcid, attrid, payload, timeout_ms, on_done)
-- print("Oops we did it again.");
-- print(on_done);
-- local ivkid = SVCD.ivkid
-- SVCD.ivkid = SVCD.ivkid + 1
-- if SVCD.ivkid > 65535 then
-- SVCD.ivkid = 0
-- end
-- SVCD.handlers[ivkid] = on_done
-- storm.os.invokeLater(timeout_ms*storm.os.MILLISECOND, function()
-- print("We are not worthy");
-- if SVCD.handlers[ivkid] ~= nil then
-- print("We are not worthy^2");
-- SVCD.handlers[ivkid](SVCD.TIMEOUT)
-- SVCD.handlers[ivkid] = nil
-- end
-- end)
-- storm.net.sendto(SVCD.wcsock, storm.mp.pack({svcid, attrid, ivkid, payload}), targetip, 2526)
-- end

-- svcd_write c impl. temporary function -- working on anonymous function calling
-- SVCD.write_invoke_later = function(ivkid, timeout_ms)
-- print("We are not worthy^2");
-- storm.os.invokeLater(timeout_ms*storm.os.MILLISECOND, function()
-- if SVCD.handlers[ivkid] ~= nil then
-- print("We are not worthy");
-- SVCD.handlers[ivkid](SVCD.TIMEOUT)
-- SVCD.handlers[ivkid] = nil
-- end
-- end)
-- end


-- Add a new service to the service daemon
-- this must be called before SVCD.advertise()
Expand Down
166 changes: 164 additions & 2 deletions natlib/svcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


#define SVCD_SYMBOLS \
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) },
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) },\
{ LSTRKEY( "svcd_test"), LFUNCVAL ( svcd_test ) },\
{ LSTRKEY( "svcd_write"), LFUNCVAL ( svcd_write )},


//If this file is defining only specific functions, or if it
Expand All @@ -29,6 +31,7 @@ static const LUA_REG_TYPE svcd_meta_map[] =
// SVCD.init() implementation
// Maintainer: Michael Andersen <[email protected]>
/////////////////////////////////////////////////////////////
static int svcd_write( lua_State* L );

// The anonymous func in init that allows for dynamic binding of advert_received
static int svcd_init_adv_received( lua_State *L )
Expand Down Expand Up @@ -91,10 +94,14 @@ static int svcd_init( lua_State *L )
//Load the SVCD table that Lua created
//This will be index 3
lua_getglobal(L, "SVCD");
printf("Put table at %d\n", lua_gettop(L));
#endif
//Now begins the part that corresponds with the lua init function

//SVCD.write
lua_pushstring(L, "write");
lua_pushlightfunction(L, svcd_write);
lua_settable(L, 3); // Store it in the table

//SVCD.asock
lua_pushstring(L, "asock");
lua_pushlightfunction(L, libstorm_net_udpsocket);
Expand Down Expand Up @@ -185,3 +192,158 @@ static int svcd_init( lua_State *L )

return 0;
}

//////////////////////////////////////////////////////////////////////////////
// SVCD.write((string targetip, number svcid, number attrid, lightfunction payload,
// number timeout_ms, lightfunction on_done))
// Authors: Aparna Dhinakaran, Michael Ho, Romi Phadte, Cesar Torres
/////////////////////////////////////////////////////////////

void resolve_table(lua_State *L, char* key){
// push the global table, resolve it and then pop it off the stack
lua_getglobal(L, "SVCD");
lua_pushstring(L, key);
lua_gettable(L, -2);
lua_insert(L, -2);
lua_settop(L, -2);
}

static int svcd_test(lua_State* L){
int numargs = lua_gettop(L);
if (numargs == 2) {
int timeout_ms = (int)lua_tonumber(L, 1);

lua_pushlightfunction(L, libstorm_os_invoke_later);
lua_pushnumber(L, timeout_ms * SECOND_TICKS);
lua_pushvalue(L, 2);
lua_call(L, 2, 0);
}
return 0;
}


static int timeout_func(lua_State* L){
int ivkid = lua_tonumber(L, lua_upvalueindex(1));
resolve_table(L, "TIMEOUT");
int timeout = lua_tonumber(L, -1);

// SVCD.handlers[ivkid] ~= nil
resolve_table(L, "handlers");
lua_pushnumber(L, ivkid);
lua_gettable(L, -2);

if(!lua_isnil(L, -1)){
lua_pushnumber(L, timeout);
lua_call(L, 1, 0);

resolve_table(L, "handlers");
lua_pushnumber(L, ivkid);
lua_gettable(L, -2);
lua_pushnil(L);
lua_settable(L, -2);
}
return 1;
}

static int svcd_write( lua_State *L )
{

int numargs = lua_gettop(L); // 6
if (numargs == 6) {
/* GET ALL THE ARGS */
size_t g;
const char* targetip = (char*) lua_tolstring(L, 1, &g);
int svcid = (int)lua_tonumber(L, 2);
int attrid = (int)lua_tonumber(L, 3);

size_t l;
const char* payload = lua_tolstring(L, 4, &l);

int timeout_ms = (int)lua_tonumber(L, 5);

/* SET IVKID */
resolve_table(L, "ivkid"); //7
int ivkid = (int) lua_tonumber(L, -1);


ivkid = ivkid + 1;
if( ivkid > 65535 ) {
ivkid = 0;
}

lua_settop(L, numargs);

// setting ivkid
lua_getglobal(L, "SVCD");
lua_pushnumber(L, ivkid);
lua_setfield(L, -2, "ivkid");
lua_settop(L, numargs);
/* END SET IVKID*/

/* SET HANDLER */
resolve_table(L, "handlers"); // 7
lua_pushnumber(L, ivkid);
lua_pushvalue(L, 6); // the ondone function
lua_settable(L, -3);
/* END SET HANDLER */

// /* LUA WRITE INVOKE */
// resolve_table(L, "write_invoke_later"); // 9
// //function write invoke later
// lua_pushnumber(L, ivkid); // 10
// lua_pushnumber(L, timeout_ms);
// lua_call(L, 2, 0);

lua_pushlightfunction(L, libstorm_os_invoke_later);
lua_pushnumber(L, timeout_ms * MILLISECOND_TICKS);
lua_pushnumber(L, ivkid);
lua_pushcclosure(L, &timeout_func, 1);

lua_call(L, 2, 0);

/* END LUA WRITE INVOKE */


/* SEND TO */
lua_pushlightfunction(L, libstorm_net_sendto);
resolve_table(L, "wcsock");
/* BEGIN MESSAGE PACK */
lua_pushlightfunction(L, libmsgpack_mp_pack);
lua_newtable(L);
//svcid
lua_pushnumber(L, 1);
lua_pushnumber(L, svcid);
lua_settable(L, -3);
//attrid
lua_pushnumber(L, 2);
lua_pushnumber(L, attrid);
lua_settable(L, -3);

// ivkid
lua_pushnumber(L, 3);
lua_pushnumber(L, ivkid); //14
lua_settable(L, -3);

//payload
lua_pushnumber(L, 4);
lua_pushlstring(L, payload, l);
lua_settable(L, -3);

lua_call(L, 1, 1);

/* END OF MSG PACK FUNCTION */

// targetip
lua_pushlstring(L, targetip, g);
//2526
lua_pushnumber(L, 2526);

lua_call(L, 4, 0);
/* END SEND_TO */
}
printf("End\n");
return 0; //return # of arguments
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job on writing the function. I think it would be better to remove commented code, print statements, etc from the code before we can merge it into master.

Untitled