-
Notifications
You must be signed in to change notification settings - Fork 30
SVCD::subscribe [NOT READY] #37
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -185,3 +186,50 @@ static int svcd_init( lua_State *L ) | |
|
||
return 0; | ||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checkinteger is really only meant to be used for parameters. I think you probably want lua_tointeger, or lua_tonumber There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
lua_pop(L, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit-picking, but I don't think you need to do this per se. Can't you just leave this on the stack and have it cleaned up later?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to stack cleanup to the lua call machinery |
||
|
||
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_settable(L, -3); | ||
lua_pop(L, 2); | ||
|
||
|
||
lua_pushlightfunction(L, libstorm_net_sendto); | ||
|
||
lua_getglobal(L, "SVCD"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have already gotten SVCD in the beginning of the function. If you know that you might need it again, why not just keeping it in the memory and recycle it here? Untitled There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to stack cleanup to the lua call machinery |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. definitely do not want to use checknumber here. use tonumber() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is incorrect code for ARM. You are attempting to write a uint16_t to an unaligned section of an unaligned array. At best, the compiler will happen to emit instructions that can support unaligned writes, at worst you will crash the processor with a Hard Fault. Memory alignment is a subtle problem, so don't feel bad. You have a couple possible solutions:
ignore the first byte, set the second byte to 1 and then write your uint16_t's as you have done - this way they will be aligned There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checknumber fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alignment issues addressed |
||
((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); | ||
lua_pushnumber(L, ivkid); | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should return the number of results (1 in this case). men in #000000 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you removed SVCD before constructing your message and sending it & wasn't sure if you could do that. Sincerely, Running with Scissors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed return value |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job! much nicer function than gettable()