Skip to content

Commit

Permalink
Lua stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dzurikmiroslav committed Nov 14, 2024
1 parent 3fdcaa3 commit fc0a9ef
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 147 deletions.
1 change: 0 additions & 1 deletion components/script/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ set(srcs
"src/l_aux_lib.c"
"src/l_board_config_lib.c"
"src/l_serial_lib.c"
"src/l_modbus_lib.c"
)

idf_component_register(SRC_DIRS "src" "lib/lua" "lib/yaml/src"
Expand Down
78 changes: 37 additions & 41 deletions components/script/src/l_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,55 +115,52 @@ static void l_process_params(lua_State* L, component_param_list_t* param_list)
lua_remove(L, -2);
}

static void component_process_params(lua_State* L, component_entry_t* component_entry)
static void component_process_params(lua_State* L, component_entry_t* component)
{
luaL_unref(L, LUA_REGISTRYINDEX, component_entry->params_ref);
luaL_unref(L, LUA_REGISTRYINDEX, component->params_ref);

component_param_list_t* param_list = component_params_read(component_entry->id);
component_param_list_t* param_list = component_params_read(component->id);

lua_rawgeti(L, LUA_REGISTRYINDEX, component_entry->params_def_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, component->params_def_ref);

l_process_params(L, param_list);
component_entry->params_ref = luaL_ref(L, LUA_REGISTRYINDEX);
component->params_ref = luaL_ref(L, LUA_REGISTRYINDEX);

if (param_list) component_params_free(param_list);
}

static void component_restart_coroutine(lua_State* L, component_entry_t* component_entry)
static void component_restart_coroutine(lua_State* L, component_entry_t* component)
{
// end previous coroutine
lua_rawgeti(L, LUA_REGISTRYINDEX, component_entry->coroutine_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, component->coroutine_ref);
if (lua_isthread(L, -1)) {
lua_State* co = lua_tothread(L, -1);
if (lua_status(co) == LUA_YIELD) {
int nresults = 0;
lua_pushboolean(co, false);
int status = lua_resume(co, L, 1, &nresults);
lua_pop(co, nresults);

if (status == LUA_YIELD) {
ESP_LOGI(TAG, "Coroutine not stop itself");
lua_closethread(L, co);
}
lua_closethread(L, co);
} else {
lua_pop(L, 1);
}
} else {
lua_pop(L, 1);
}
lua_pop(L, 1);

luaL_unref(L, LUA_REGISTRYINDEX, component_entry->coroutine_ref);
component_entry->coroutine_ref = LUA_NOREF;
component_entry->resume_after = 0;
luaL_unref(L, LUA_REGISTRYINDEX, component->coroutine_ref);
component->coroutine_ref = LUA_NOREF;
component->resume_after = 0;

lua_gc(L, LUA_GCCOLLECT, 0);

// start new coroutine
lua_rawgeti(L, LUA_REGISTRYINDEX, component_entry->start_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, component_entry->params_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, component->start_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, component->params_ref);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
const char* err = lua_tostring(L, -1);
lua_writestring(err, strlen(err));
lua_writeline();
lua_pop(L, 1);
} else {
if (lua_isthread(L, -1)) {
component_entry->coroutine_ref = luaL_ref(L, LUA_REGISTRYINDEX);
component->coroutine_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
lua_pop(L, 1);
}
Expand Down Expand Up @@ -202,28 +199,28 @@ static int l_add_component(lua_State* L)
lua_pop(L, 1);

component_list_t* component_list = get_component_list(L);
component_entry_t* component_entry;
SLIST_FOREACH (component_entry, component_list, entries) {
if (strcmp(id, component_entry->id) == 0) {
component_entry_t* component;
SLIST_FOREACH (component, component_list, entries) {
if (strcmp(id, component->id) == 0) {
luaL_argerror(L, 1, "component with duplicate id");
}
}

component_entry = (component_entry_t*)malloc(sizeof(component_entry_t));
component_entry->id = strdup(id);
component_entry->name = strdup(name);
component_entry->description = description ? strdup(description) : NULL;
component = (component_entry_t*)malloc(sizeof(component_entry_t));
component->id = strdup(id);
component->name = strdup(name);
component->description = description ? strdup(description) : NULL;
lua_getfield(L, 1, "params");
component_entry->params_def_ref = luaL_ref(L, LUA_REGISTRYINDEX);
component->params_def_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_getfield(L, 1, "start");
component_entry->start_ref = luaL_ref(L, LUA_REGISTRYINDEX);
component_entry->params_ref = LUA_NOREF;
component_entry->coroutine_ref = LUA_NOREF;
component->start_ref = luaL_ref(L, LUA_REGISTRYINDEX);
component->params_ref = LUA_NOREF;
component->coroutine_ref = LUA_NOREF;

component_process_params(L, component_entry);
component_restart_coroutine(L, component_entry);
component_process_params(L, component);
component_restart_coroutine(L, component);

SLIST_INSERT_HEAD(component_list, component_entry, entries);
SLIST_INSERT_HEAD(component_list, component, entries);

return 0;
}
Expand Down Expand Up @@ -262,20 +259,19 @@ void l_component_register(lua_State* L)
components_ref = luaL_ref(L, LUA_REGISTRYINDEX);
}

void l_component_resume(lua_State* L, bool finalize)
void l_component_resume(lua_State* L)
{
component_list_t* component_list = get_component_list(L);
component_entry_t* component;
SLIST_FOREACH (component, component_list, entries) {
if (finalize || component->resume_after == 0 || component->resume_after < xTaskGetTickCount()) {
if (component->resume_after == 0 || component->resume_after < xTaskGetTickCount()) {
lua_rawgeti(L, LUA_REGISTRYINDEX, component->coroutine_ref);
if (lua_isthread(L, -1)) {
lua_State* co = lua_tothread(L, -1);
int status = lua_status(co);
if (status == LUA_YIELD || status == LUA_OK) {
int nresults = 0;
lua_pushboolean(co, !finalize);
status = lua_resume(co, L, 1, &nresults);
status = lua_resume(co, L, 0, &nresults);
if (status == LUA_YIELD || status == LUA_OK) {
if (nresults > 0 && lua_isinteger(co, -nresults)) {
component->resume_after = xTaskGetTickCount() + pdMS_TO_TICKS(lua_tointeger(co, -nresults));
Expand Down
2 changes: 1 addition & 1 deletion components/script/src/l_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

void l_component_register(lua_State* L);

void l_component_resume(lua_State* L, bool finalize);
void l_component_resume(lua_State* L);

script_component_list_t* l_component_get_components(lua_State* L);

Expand Down
42 changes: 0 additions & 42 deletions components/script/src/l_modbus_lib.c

This file was deleted.

8 changes: 0 additions & 8 deletions components/script/src/l_modbus_lib.h

This file was deleted.

91 changes: 72 additions & 19 deletions components/script/src/l_serial_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@

#define BUF_SIZE 256

// static const char* TAG = "l_serial";
static const char* TAG = "l_serial";

static int l_get_available(lua_State* L)
static bool is_opened;

static int l_open(lua_State* L)
{
lua_pushboolean(L, serial_script_is_available());
ESP_LOGW(TAG, "l_open");

if (!serial_script_is_available() || is_opened) {
lua_pushnil(L);
} else {
is_opened = true;
lua_newtable(L);
luaL_setmetatable(L, "serial.port");
}

return 1;
}

static int l_read(lua_State* L)
static int l_port_read(lua_State* L)
{
ESP_LOGW(TAG, "l_port_read");

char buf[BUF_SIZE];
size_t len = BUF_SIZE;
uint32_t timeout = 0;
if (!lua_isnoneornil(L, 1)) {
timeout = luaL_checkinteger(L, 1);
}

if (serial_script_read(buf, &len, timeout) != ESP_OK) {
if (serial_script_read(buf, &len) != ESP_OK) {
luaL_error(L, "serial error");
}

Expand All @@ -38,38 +47,82 @@ static int l_read(lua_State* L)
return 1;
}

static int l_write(lua_State* L)
static int l_port_write(lua_State* L)
{
ESP_LOGW(TAG, "l_port_write");

size_t len;
const char* buf;
char buf_num;

if (lua_isnumber(L, 1)) {
buf_num = lua_tonumber(L, 1);
if (lua_isnumber(L, 2)) {
buf_num = lua_tonumber(L, 2);
len = 1;
buf = &buf_num;
} else if (lua_isstring(L, 1)) {
buf = lua_tolstring(L, 1, &len);
} else if (lua_isstring(L, 2)) {
buf = lua_tolstring(L, 2, &len);
} else {
luaL_error(L, "argument must be number or string");
luaL_argerror(L, 2, "argument must be number or string");
return 0;
}

serial_script_write(buf, len);
if (serial_script_write(buf, len) != ESP_OK) {
luaL_error(L, "serial error");
}

return 0;
}

static int l_port_flush(lua_State* L)
{
ESP_LOGW(TAG, "l_port_flush");

if (serial_script_flush() != ESP_OK) {
luaL_error(L, "serial error");
}

return 0;
}

static int l_port_gc(lua_State* L)
{
ESP_LOGW(TAG, "l_port_gc");

is_opened = false;

return 0;
}

static const luaL_Reg lib[] = {
{ "getavailable", l_get_available },
{ "read", l_read },
{ "write", l_write },
{ "open", l_open },
{ NULL, NULL },
};

static const luaL_Reg port_fields[] = {
{ "write", l_port_write },
{ "read", l_port_read },
{ "flush", l_port_flush },
{ NULL, NULL },
};

static const luaL_Reg port_metadata[] = {
{ "__index", NULL },
{ "__gc", l_port_gc },
{ NULL, NULL },
};

int luaopen_serial(lua_State* L)
{
is_opened = false;

luaL_newlib(L, lib);

luaL_newmetatable(L, "serial.port");
luaL_setfuncs(L, port_metadata, 0);
luaL_newlibtable(L, port_fields);
luaL_setfuncs(L, port_fields, 0);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);

return 1;
}
10 changes: 1 addition & 9 deletions components/script/src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "l_component.h"
#include "l_evse_lib.h"
#include "l_json_lib.h"
#include "l_modbus_lib.h"
#include "l_mqtt_lib.h"
#include "l_serial_lib.h"
#include "lauxlib.h"
Expand All @@ -25,9 +24,7 @@
#include "script_utils.h"
#include "script_watchdog.h"

#define START_TIMEOUT 10000
#define SHUTDOWN_TIMEOUT 1000
#define HEARTBEAT_THRESHOLD 5
#define OUTPUT_BUFFER_SIZE 4096

#define NVS_NAMESPACE "script"
Expand Down Expand Up @@ -83,9 +80,6 @@ static void script_task_func(void* param)
luaL_requiref(L, "serial", luaopen_serial, 0);
lua_pop(L, 1);

luaL_requiref(L, "modbus", luaopen_modbus, 0);
lua_pop(L, 1);

lua_gc(L, LUA_GCSETPAUSE, 110);
lua_gc(L, LUA_GCSETSTEPMUL, 200);

Expand All @@ -108,7 +102,7 @@ static void script_task_func(void* param)
}
xSemaphoreTake(script_mutex, portMAX_DELAY);
script_watchdog_reset();
l_component_resume(L, false);
l_component_resume(L);
xSemaphoreGive(script_mutex);

int top = lua_gettop(L);
Expand All @@ -119,8 +113,6 @@ static void script_task_func(void* param)
vTaskDelay(pdMS_TO_TICKS(50));
}

l_component_resume(L, true);

lua_close(L);
L = NULL;

Expand Down
Loading

0 comments on commit fc0a9ef

Please sign in to comment.