36 #define LUA_PUSHT_INT(l, k, v) do { \
37 lua_pushliteral(luastate, k); \
38 lua_pushinteger(luastate, v); \
39 lua_settable(luastate, -3); \
42 static void SCLuaPushTableBoolean(
lua_State *L,
const char *key,
bool val)
44 lua_pushstring(L, key);
45 lua_pushboolean(L, val);
71 #define DNP3_MAX_EAGER_POINTS 32
72 #define DNP3_POINTS_MT "sc.dnp3.points"
95 static int Dnp3PointsLen(
lua_State *luastate)
103 return luaL_error(luastate,
"invalid dnp3 points handle");
105 lua_pushinteger(luastate, pts->
n_points);
110 static int Dnp3PointsIndex(
lua_State *luastate)
118 lua_pushnil(luastate);
121 const lua_Integer i = lua_tointeger(luastate, 2);
124 if (i < 1 || (uint64_t)i > pts->
n_points) {
125 lua_pushnil(luastate);
151 while (idx < (uint32_t)i) {
152 point = point->next.tqe_next;
160 while (idx > (uint32_t)i) {
170 lua_pushnil(luastate);
183 lua_pushnil(luastate);
188 while (point != NULL && idx < (uint32_t)i) {
189 point = point->next.tqe_next;
193 lua_pushnil(luastate);
199 lua_pushnil(luastate);
203 lua_newtable(luastate);
204 lua_pushinteger(luastate, point->
index);
205 lua_setfield(luastate, -2,
"index");
225 static int Dnp3PointsNext(
lua_State *luastate)
232 const lua_Integer pos = lua_tointeger(luastate, 2);
233 if (pos < 0 || (uint64_t)pos + 1 > pts->
n_points) {
270 while (point != NULL && idx <= (uint32_t)pos) {
271 point = point->next.tqe_next;
282 lua_pushinteger(luastate, pos + 1);
283 lua_newtable(luastate);
284 lua_pushinteger(luastate, point->
index);
285 lua_setfield(luastate, -2,
"index");
300 static int Dnp3PointsPairs(
lua_State *luastate)
305 lua_pushnil(luastate);
308 lua_pushcfunction(luastate, Dnp3PointsNext);
310 lua_pushvalue(luastate, 1);
311 lua_pushinteger(luastate, 0);
315 static void DNP3PushObjectPoints(
lua_State *luastate,
DNP3Object *
object, uint32_t object_idx)
326 lua_newtable(luastate);
330 lua_pushinteger(luastate, i);
331 lua_newtable(luastate);
332 lua_pushinteger(luastate,
p->index);
333 lua_setfield(luastate, -2,
"index");
335 lua_settable(luastate, -3);
345 lua_pushnil(luastate);
365 lua_pushliteral(luastate,
"__index");
366 lua_pushcfunction(luastate, Dnp3PointsIndex);
367 lua_settable(luastate, -3);
368 lua_pushliteral(luastate,
"__len");
369 lua_pushcfunction(luastate, Dnp3PointsLen);
370 lua_settable(luastate, -3);
371 lua_pushliteral(luastate,
"__pairs");
372 lua_pushcfunction(luastate, Dnp3PointsPairs);
373 lua_settable(luastate, -3);
377 lua_setmetatable(luastate, -2);
381 static void DNP3PushObjects(
lua_State *luastate, DNP3ObjectList *objects)
385 uint32_t obj_idx = 0;
388 lua_pushinteger(luastate, i++);
389 lua_newtable(luastate);
391 lua_pushliteral(luastate,
"group");
392 lua_pushinteger(luastate, object->
group);
393 lua_settable(luastate, -3);
395 lua_pushliteral(luastate,
"variation");
396 lua_pushinteger(luastate, object->
variation);
397 lua_settable(luastate, -3);
399 lua_pushliteral(luastate,
"points");
400 DNP3PushObjectPoints(luastate,
object, obj_idx++);
401 lua_settable(luastate, -3);
403 lua_settable(luastate, -3);
407 static void DNP3PushLinkHeader(
lua_State *luastate, DNP3LinkHeader *header)
416 static void DNP3PushApplicationHeader(
lua_State *luastate,
417 DNP3ApplicationHeader *header)
420 LUA_PUSHT_INT(luastate,
"function_code", header->function_code);
426 lua_pushliteral(luastate,
"link_header");
427 lua_newtable(luastate);
428 DNP3PushLinkHeader(luastate, &tx->
lh);
429 lua_settable(luastate, -3);
435 lua_pushliteral(luastate,
"application_header");
436 lua_newtable(luastate);
437 DNP3PushApplicationHeader(luastate, &tx->
ah);
438 lua_settable(luastate, -3);
440 lua_pushliteral(luastate,
"objects");
441 lua_newtable(luastate);
442 DNP3PushObjects(luastate, &tx->
objects);
443 lua_settable(luastate, -3);
449 lua_pushliteral(luastate,
"link_header");
450 lua_newtable(luastate);
451 DNP3PushLinkHeader(luastate, &tx->
lh);
452 lua_settable(luastate, -3);
458 lua_pushliteral(luastate,
"application_header");
459 lua_newtable(luastate);
460 DNP3PushApplicationHeader(luastate, &tx->
ah);
461 lua_settable(luastate, -3);
466 lua_pushliteral(luastate,
"objects");
467 lua_newtable(luastate);
468 DNP3PushObjects(luastate, &tx->
objects);
469 lua_settable(luastate, -3);
472 static int DNP3GetTx(
lua_State *luastate)
483 lua_newtable(luastate);
485 lua_pushliteral(luastate,
"tx_num");
486 lua_pushinteger(luastate, tx->
tx_num);
487 lua_settable(luastate, -3);
489 SCLuaPushTableBoolean(luastate,
"is_request", tx->
is_request);
491 lua_pushliteral(luastate,
"request");
492 lua_newtable(luastate);
493 SCLuaPushTableBoolean(luastate,
"done", tx->
done);
494 SCLuaPushTableBoolean(luastate,
"complete", tx->
complete);
495 DNP3PushRequest(luastate, tx);
496 lua_settable(luastate, -3);
498 lua_pushliteral(luastate,
"response");
499 lua_newtable(luastate);
500 SCLuaPushTableBoolean(luastate,
"done", tx->
done);
501 SCLuaPushTableBoolean(luastate,
"complete", tx->
complete);
502 DNP3PushResponse(luastate, tx);
503 lua_settable(luastate, -3);
509 static const struct luaL_Reg dnp3lib[] = {
511 {
"get_tx", DNP3GetTx, },
518 luaL_newlib(L, dnp3lib);