From ecf02a74797ab4edd4b91305bc765e4a2e1e7e3d Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Mon, 22 Dec 2025 16:10:33 +0100 Subject: [PATCH] transition to lua based --- .vscode/settings.json | 18 +++++++++ src/ascii_cmd.c | 48 ----------------------- src/command.lua | 91 +++++++++++++++++++++++++++++++++++++------ src/lua_rds.c | 60 ++++++++++++++++++++++++++-- src/lua_rds.h | 1 - src/udp_server.c | 2 +- 6 files changed, 155 insertions(+), 65 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 99777a0..9a79bd3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,5 +24,23 @@ }, "C_Cpp.codeAnalysis.clangTidy.checks.disabled": [ "clang-diagnostic-error" + ], + "Lua.diagnostics.globals": [ + "set_rds_pi", + "set_rds_ecc", + "str", + "cmd", + "set_rds_pty", + "set_rds_slc_data", + "set_rds_ct", + "set_rds_dpty", + "set_rds_tp", + "set_rds_ta", + "set_rds_rt1_enabled", + "set_rds_rt2_enabled", + "set_rds_ptyn_enabled", + "set_rds_rt_type", + "set_rds_rds2mod", + "set_rds_rdsgen" ] } \ No newline at end of file diff --git a/src/ascii_cmd.c b/src/ascii_cmd.c index 2e85ef4..375591c 100644 --- a/src/ascii_cmd.c +++ b/src/ascii_cmd.c @@ -74,26 +74,6 @@ static void handle_##name(char *arg, RDSModulator* mod, char* output) { \ AF_HANDLER(af, RDSAFs, af, add_rds_af) AF_HANDLER(afo, RDSAFsODA, af_oda, add_rds_af_oda) -SIMPLE_INT_HANDLER(pty, pty) -SIMPLE_INT_HANDLER(ct, ct) -SIMPLE_INT_HANDLER(dpty, dpty) -SIMPLE_INT_HANDLER(tp, tp) -SIMPLE_INT_HANDLER(ta, ta) -SIMPLE_INT_HANDLER(rt1en, rt1_enabled) -SIMPLE_INT_HANDLER(rt2en, rt2_enabled) -SIMPLE_INT_HANDLER(ptynen, ptyn_enabled) -SIMPLE_INT_HANDLER(rttype, rt_type) - -static void handle_rds2mod(char *arg, RDSModulator* mod, char* output) { - mod->enc->encoder_data.rds2_mode = atoi(arg); - strcpy(output, "+"); -} -static void handle_rdsgen(char *arg, RDSModulator* mod, char* output) { - mod->params.rdsgen = atoi(arg); - strcpy(output, "+"); -} - -STRING_HANDLER(ptyn, PTYN_LENGTH, set_rds_ptyn) STRING_HANDLER(ps, PS_LENGTH, set_rds_ps) STRING_HANDLER(tps, PS_LENGTH, set_rds_tps) STRING_HANDLER(rt1, RT_LENGTH, set_rds_rt1) @@ -102,9 +82,6 @@ STRING_HANDLER(rt2, RT_LENGTH, set_rds_rt2) RAW_STRING_HANDLER(lps, LPS_LENGTH, set_rds_lps) RAW_STRING_HANDLER(ert, ERT_LENGTH, set_rds_ert) -HEX_HANDLER(ecc, ecc) -HEX_HANDLER(slcd, slc_data) - static void handle_udg(char *arg, char *pattern, RDSModulator* mod, char* output) { uint8_t all_scanned = 1, bad_format = 0; uint16_t blocks[8][3]; @@ -199,16 +176,6 @@ static void handle_link(char *arg, RDSModulator* mod, char* output) { strcpy(output, "+"); } -static void handle_pi(char *arg, RDSModulator* mod, char* output) { - uint16_t pi_value = strtoul(arg, NULL, 16); - if ((pi_value & 0xF000) == 0) { - strcpy(output, "-"); - return; - } - mod->enc->data[mod->enc->program].pi = pi_value; - strcpy(output, "+"); -} - static void handle_adr(char *arg, RDSModulator* mod, char* output) { uint16_t ids[2]; int count = sscanf(arg, "%4hu,%4hu", &ids[0], &ids[1]); @@ -418,10 +385,6 @@ static void handle_eondt(char *arg, char *pattern, RDSModulator* mod, char* outp static const command_handler_t commands_eq3[] = { {"PS", handle_ps, 2}, - {"PI", handle_pi, 2}, - {"TP", handle_tp, 2}, - {"TA", handle_ta, 2}, - {"CT", handle_ct, 2}, {"AF", handle_af, 2} }; @@ -429,8 +392,6 @@ static const command_handler_t commands_eq4[] = { {"TPS", handle_tps, 3}, {"RT1", handle_rt1, 3}, {"RT2", handle_rt2, 3}, - {"PTY", handle_pty, 3}, - {"ECC", handle_ecc, 3}, {"RTP", handle_rtp, 3}, {"LPS", handle_lps, 3}, {"ERT", handle_ert, 3}, @@ -440,9 +401,6 @@ static const command_handler_t commands_eq4[] = { static const command_handler_t commands_eq5[] = { {"TEXT", handle_rt1, 4}, - {"PTYN", handle_ptyn, 4}, - {"DPTY", handle_dpty, 4}, - {"SLCD", handle_slcd, 4}, {"ERTP", handle_ertp, 4}, {"LINK", handle_link, 4}, {"SITE", handle_site, 4} @@ -453,23 +411,17 @@ static const command_handler_t commands_eq2[] = { }; static const command_handler_t commands_eq6[] = { - {"RT1EN", handle_rt1en, 5}, - {"RT2EN", handle_rt2en, 5}, {"RTPER", handle_rtper, 5}, {"LEVEL", handle_level, 5}, }; static const command_handler_t commands_eq7[] = { - {"PTYNEN", handle_ptynen, 6}, {"RTPRUN", handle_rtprun, 6}, {"GRPSEQ", handle_grpseq, 6}, - {"RDSGEN", handle_rdsgen, 6}, - {"RTTYPE", handle_rttype, 6}, }; static const command_handler_t commands_eq8[] = { {"PROGRAM", handle_program, 7}, - {"RDS2MOD", handle_rds2mod, 7}, {"GRPSEQ2", handle_grpseq2, 7}, {"DTTMOUT", handle_dttmout, 7}, {"ERTPRUN", handle_ertprun, 7}, diff --git a/src/command.lua b/src/command.lua index 4c199b6..d2febc9 100644 --- a/src/command.lua +++ b/src/command.lua @@ -1,11 +1,80 @@ --- if type(cmd) == "string" then --- if cmd:sub(1, 3) == "PI=" then --- local hex = cmd:sub(7) --- local pi = tonumber(hex, 16) --- if (pi & 0xf000) == 0 then return "-" end --- if pi then --- set_rds_pi(pi) --- return "+" --- end --- end --- end \ No newline at end of file +do return "" end + +if type(cmd) == "string" then + local cmd, value = cmd:match("([^=]+)=([^=]+)") + cmd = cmd:lower() + if cmd == "pi" then + local pi = tonumber(value, 16) + if not pi then return "-" end + if (pi & 0xF000) == 0 then return "-" end + set_rds_pi(pi) + return "+" + elseif cmd == "ecc" then + local ecc = tonumber(value, 16) + if not ecc then return "-" end + set_rds_ecc(ecc) + return "+" + elseif cmd == "pty" then + local pty = tonumber(value) + if not pty then return "-" end + set_rds_pty(pty) + return "+" + elseif cmd == "slcd" then + local slc_data = tonumber(value, 16) + if not slc_data then return "-" end + set_rds_slc_data(slc_data) + return "+" + elseif cmd == "ct" then + local ct = tonumber(value) + if not ct then return "-" end + set_rds_ct(ct) + return "+" + elseif cmd == "dpty" then + local dpty = tonumber(value) + if not dpty then return "-" end + set_rds_dpty(dpty) + return "+" + elseif cmd == "tp" then + local tp = tonumber(value) + if not tp then return "-" end + set_rds_tp(tp) + return "+" + elseif cmd == "ta" then + local ta = tonumber(value) + if not ta then return "-" end + set_rds_ta(ta) + return "+" + elseif cmd == "rt1en" then + local en = tonumber(value) + if not en then return "-" end + set_rds_rt1_enabled(en) + return "+" + elseif cmd == "rt2en" then + local en = tonumber(value) + if not en then return "-" end + set_rds_rt2_enabled(en) + return "+" + elseif cmd == "ptynen" then + local en = tonumber(value) + if not en then return "-" end + set_rds_ptyn_enabled(en) + return "+" + elseif cmd == "rttype" then + local type = tonumber(value) + if not type then return "-" end + set_rds_rt_type(type) + return "+" + elseif cmd == "rds2mod" then + local type = tonumber(value) + if not type then return "-" end + set_rds_rds2mod(type) + return "+" + elseif cmd == "rdsgen" then + local type = tonumber(value) + if not type then return "-" end + set_rds_rdsgen(type) + return "+" + else + return "?" + end +end \ No newline at end of file diff --git a/src/lua_rds.c b/src/lua_rds.c index 279c5db..83226fb 100644 --- a/src/lua_rds.c +++ b/src/lua_rds.c @@ -3,17 +3,69 @@ static RDSModulator* mod = NULL; static lua_State *L = NULL; -int lua_set_rds_pi(lua_State *localL) { - int pi_value = luaL_checkinteger(localL, 1); - mod->enc->data[mod->enc->program].pi = pi_value; +#define INT_NONRETURN_HANDLER(name) \ +int lua_set_rds_##name(lua_State *localL) { \ + mod->enc->data[mod->enc->program].name = luaL_checkinteger(localL, 1); \ + return 0; \ +} +#define STR_NONRETURN_HANDLER(name, function) \ +int lua_set_rds_##name(lua_State *localL) { \ + const char* str = luaL_checklstring(L, 1); \ + function(mod->enc, str); \ + return 0; \ +} + + +INT_NONRETURN_HANDLER(pi) +INT_NONRETURN_HANDLER(pty) +INT_NONRETURN_HANDLER(ecc) +INT_NONRETURN_HANDLER(slc_data) +INT_NONRETURN_HANDLER(ct) +INT_NONRETURN_HANDLER(dpty) +INT_NONRETURN_HANDLER(tp) +INT_NONRETURN_HANDLER(ta) +INT_NONRETURN_HANDLER(rt1_enabled) +INT_NONRETURN_HANDLER(rt2_enabled) +INT_NONRETURN_HANDLER(ptyn_enabled) +INT_NONRETURN_HANDLER(rt_type) +int lua_set_rds_rds2mod(lua_State *localL) { + mod->enc->encoder_data.rds2_mode = luaL_checkinteger(localL, 1); return 0; } +int lua_set_rds_rdsgen(lua_State *localL) { + mod->params.rdsgen = luaL_checkinteger(localL, 1); + return 0; +} + +STR_NONRETURN_HANDLER(ptyn, set_rds_ptyn) void init_lua(RDSModulator* rds_mod) { mod = rds_mod; L = luaL_newstate(); - luaL_openlibs(L); + + luaL_requiref(L, "_G", luaopen_base, 1); + luaL_requiref(L, LUA_STRLIBNAME, luaopen_string, 1); + luaL_requiref(L, LUA_TABLIBNAME, luaopen_table, 1); + luaL_requiref(L, LUA_UTF8LIBNAME, luaopen_utf8, 1); + luaL_requiref(L, LUA_COLIBNAME, luaopen_coroutine, 1); + luaL_requiref(L, LUA_MATHLIBNAME, luaopen_math, 1); + lua_pop(L, 6); + lua_register(L, "set_rds_pi", lua_set_rds_pi); + lua_register(L, "set_rds_pty", lua_set_rds_pty); + lua_register(L, "set_rds_ecc", lua_set_rds_ecc); + lua_register(L, "set_rds_slc_data", lua_set_rds_slc_data); + lua_register(L, "set_rds_ct", lua_set_rds_ct); + lua_register(L, "set_rds_dpty", lua_set_rds_dpty); + lua_register(L, "set_rds_tp", lua_set_rds_tp); + lua_register(L, "set_rds_ta", lua_set_rds_ta); + lua_register(L, "set_rds_rt1_enabled", lua_set_rds_rt1_enabled); + lua_register(L, "set_rds_rt2_enabled", lua_set_rds_rt2_enabled); + lua_register(L, "set_rds_ptyn_enabled", lua_set_rds_ptyn_enabled); + lua_register(L, "set_rds_rt_type", lua_set_rds_rt_type); + lua_register(L, "set_rds_rds2mod", lua_set_rds_rds2mod); + lua_register(L, "set_rds_rdsgen", lua_set_rds_rdsgen); + lua_register(L, "set_rds_ptyn", lua_set_rds_ptyn); } void run_lua(char *str, char *cmd_output) { diff --git a/src/lua_rds.h b/src/lua_rds.h index 087ed16..ca58bb4 100644 --- a/src/lua_rds.h +++ b/src/lua_rds.h @@ -5,7 +5,6 @@ #include "rds.h" #include "modulator.h" -int lua_set_rds_pi(lua_State *L); void init_lua(RDSModulator* rds_mod); void run_lua(char *str, char *cmd_output); void destroy_lua(); \ No newline at end of file diff --git a/src/udp_server.c b/src/udp_server.c index 3ca29e4..29c9747 100644 --- a/src/udp_server.c +++ b/src/udp_server.c @@ -61,7 +61,7 @@ void poll_udp_server() { memset(cmd_output, 0, BUF_SIZE); process_ascii_cmd(mod, cmd_buf, cmd_output); - run_lua(cmd_buf, NULL); + run_lua(cmd, NULL); size_t out_len = strlen(cmd_output); if (out_len > 0) {