From 1925b09edd5ad0c0c28407fe142b67767649d2c1 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Fri, 26 Dec 2025 13:35:32 +0100 Subject: [PATCH] total control over rds2 in lua --- plugin.lua | 19 +++-- scripts/1-state.lua | 1 + scripts/99-data.lua | 192 ++++++++++++++++++++++---------------------- src/lua_rds.c | 45 +++++++++-- src/lua_rds.h | 1 + src/rds.c | 4 + src/rds.h | 5 +- 7 files changed, 155 insertions(+), 112 deletions(-) diff --git a/plugin.lua b/plugin.lua index e6b7535..4185d01 100644 --- a/plugin.lua +++ b/plugin.lua @@ -44,16 +44,23 @@ function tick() end ---@return string function data_handle(data) end ---This function is called when the group "L" is in the sequence +---Please remember that the core always fills in PTY and TP and PI in C if this is an B group ---It should be defined by the user in the script. ---@param group string group this was called in from the group sequence ----@param b integer ----@param c integer ----@param d integer ---@return boolean generated ---@return integer b ---@return integer c ---@return integer d -function group(group, b, c, d) end +function group(group) end +---This function is called when an RDS2 group is to be generated on mode 3 +---If a was returned 0, PTY and TP will be filled in, along with the PI code in C if needed +---It should be defined by the user in the script. +---@param stream integer +---@return integer a +---@return integer b +---@return integer c +---@return integer d +function rds2_group(stream) end ---@param pi integer function set_rds_pi(pi) end @@ -117,9 +124,9 @@ function set_rds_rt_type(rt_type) end function get_rds_rt_type() end -- Modulation & Generation ----@param mode boolean +---@param mode integer function set_rds2_mode(mode) end ----@return boolean +---@return integer function get_rds2_mode() end ---@param streams integer diff --git a/scripts/1-state.lua b/scripts/1-state.lua index b9d645a..3bd7424 100644 --- a/scripts/1-state.lua +++ b/scripts/1-state.lua @@ -1,3 +1,4 @@ +---@diagnostic disable: undefined-global function on_state() if get_rds_rtp_meta(false) then init_rtp() end if get_rds_rtp_meta(true) then init_ertp() end diff --git a/scripts/99-data.lua b/scripts/99-data.lua index 8cdaf6d..5e42b44 100644 --- a/scripts/99-data.lua +++ b/scripts/99-data.lua @@ -6,13 +6,13 @@ function data_handle(data) end if cmd == nil then data = data:lower() - if data == "ver" then return string.format("rds95 v. %s - (C) 2025 radio95 - lua parser", core_version) + if data == "ver" then return string.format("rds95 core v. %s - (C) 2025 radio95 - lua parser\r\n", core_version) elseif data == "init" then set_rds_program_defaults() - return "+" + return "+\r\n" elseif data == "reset" then reset_rds() - return "+" + return "+\r\n" elseif data == "pi" then return string.format("PI=%s\r\n", string.format("%x", get_rds_pi())) elseif data == "pty" then return string.format("PTY=%s\r\n", string.format("%d", get_rds_pty())) elseif data == "ecc" then return string.format("ECC=%s\r\n", string.format("%x", get_rds_ecc())) @@ -25,7 +25,7 @@ function data_handle(data) elseif data == "rt2en" then return string.format("RT2EN=%s\r\n", string.format("%d", (get_rds_rt2_enabled() and 1 or 0))) elseif data == "ptynen" then return string.format("PTYNEN=%s\r\n", string.format("%d", (get_rds_ptyn_enabled() and 1 or 0))) elseif data == "rttype" then return string.format("RTTYPE=%s\r\n", string.format("%d", get_rds_rt_type())) - elseif data == "rds2mod" then return string.format("RDS2MOD=%s\r\n", string.format("%d", (get_rds2_mode() and 1 or 0))) + elseif data == "rds2mod" then return string.format("RDS2MOD=%s\r\n", string.format("%d", get_rds2_mode())) elseif data == "rdsgen" then return string.format("RDSGEN=%s\r\n", string.format("%d", get_rds_streams())) elseif data == "level" then return string.format("LEVEL=%s\r\n", string.format("%d", get_rds_level() * 255)) elseif data == "link" then return string.format("LINK=%s\r\n", string.format("%d", (get_rds_link() and 1 or 0))) @@ -51,7 +51,7 @@ function data_handle(data) local eon_cmd, eon_num = data:match("^eon(%d+)([a-z]+)$") if eon_cmd then local eon_idx = tonumber(eon_cmd) - if not eon_idx or eon_idx < 1 or eon_idx > eon_count then return "?" end + if not eon_idx or eon_idx < 1 or eon_idx > eon_count then return "?\r\n" end eon_idx = eon_idx - 1 local enabled, pi, tp, ta, pty, ps, afs, data_val = get_rds_eon(eon_idx) @@ -64,7 +64,7 @@ function data_handle(data) elseif eon_num == "dt" then return string.format("EON%dDT=%x\r\n", eon_idx + 1, data_val) end end - return "?" + return "?\r\n" end end @@ -74,64 +74,64 @@ function data_handle(data) local eon_num, eon_type = cmd:match("^eon(%d+)([a-z]+)$") if eon_num then local eon_idx = tonumber(eon_num) - if not eon_idx or eon_idx < 1 or eon_idx > eon_count then return "?" end + if not eon_idx or eon_idx < 1 or eon_idx > eon_count then return "?\r\n" end eon_idx = eon_idx - 1 local enabled, pi, tp, ta, pty, ps, afs, data_val = get_rds_eon(eon_idx) if eon_type == "en" then local en_val = tonumber(value) - if not en_val then return "-" end + if not en_val then return "-\r\n" end enabled = (en_val ~= 0) set_rds_eon(eon_idx, enabled, pi, tp, ta, pty, ps, afs, data_val) - return "+" + return "+\r\n" elseif eon_type == "pi" then local pi_val = tonumber(value, 16) - if not pi_val then return "-" end + if not pi_val then return "-\r\n" end set_rds_eon(eon_idx, enabled, pi_val, tp, ta, pty, ps, afs, data_val) - return "+" + return "+\r\n" elseif eon_type == "ps" then local ps_val = value:sub(1, 24) set_rds_eon(eon_idx, enabled, pi, tp, ta, pty, ps_val, afs, data_val) - return "+" + return "+\r\n" elseif eon_type == "pty" then local pty_val = tonumber(value) - if not pty_val then return "-" end + if not pty_val then return "-\r\n" end set_rds_eon(eon_idx, enabled, pi, tp, ta, pty_val, ps, afs, data_val) - return "+" + return "+\r\n" elseif eon_type == "ta" then - if not enabled or not tp then return "-" end + if not enabled or not tp then return "-\r\n" end local ta_val = tonumber(value) - if not ta_val then return "-" end + if not ta_val then return "-\r\n" end ta = (ta_val ~= 0) set_rds_eon(eon_idx, enabled, pi, tp, ta, pty, ps, afs, data_val) if ta then set_rds_ta(true) end - return "+" + return "+\r\n" elseif eon_type == "tp" then local tp_val = tonumber(value) - if not tp_val then return "-" end + if not tp_val then return "-\r\n" end tp = (tp_val ~= 0) set_rds_eon(eon_idx, enabled, pi, tp, ta, pty, ps, afs, data_val) - return "+" + return "+\r\n" elseif eon_type == "af" then local af_table = {} if value == "" or value == "0" then set_rds_eon(eon_idx, enabled, pi, tp, ta, pty, ps, {}, data_val) - return "+" + return "+\r\n" end for freq_str in value:gmatch("([^,]+)") do local f = tonumber(freq_str) if f then table.insert(af_table, f) - else return "-" end + else return "-\r\n" end end - if #af_table > 25 then return "-" end + if #af_table > 25 then return "-\r\n" end set_rds_eon(eon_idx, enabled, pi, tp, ta, pty, ps, af_table, data_val) - return "+" + return "+\r\n" elseif eon_type == "dt" then local dt_val = tonumber(value, 16) - if not dt_val then return "-" end + if not dt_val then return "-\r\n" end set_rds_eon(eon_idx, enabled, pi, tp, ta, pty, ps, afs, dt_val) - return "+" - else return "?" end + return "+\r\n" + else return "?\r\n" end end local udg_num = cmd:match("^udg([12])$") @@ -142,13 +142,13 @@ function data_handle(data) for segment in value:gmatch("([^,]+)") do local b, c, d = segment:match("^(%x%x%x%x)(%x%x%x%x)(%x%x%x%x)$") - if not (b and c and d) then return "-" end + if not (b and c and d) then return "-\r\n" end table.insert(groups, {tonumber(b, 16), tonumber(c, 16), tonumber(d, 16)}) end - if #groups > 8 or #groups == 0 then return "-" end + if #groups > 8 or #groups == 0 then return "-\r\n" end set_rds_udg(xy, groups) - return "+" + return "+\r\n" end if udg2_num then local xy = (udg2_num == "1") @@ -162,164 +162,164 @@ function data_handle(data) if #groups > 8 or #groups == 0 then return "-" end set_rds_udg2(xy, groups) - return "+" + return "+\r\n" end if cmd == "pi" then local pi = tonumber(value, 16) - if not pi then return "-" end - if (pi & 0xF000) == 0 then return "-" end + if not pi then return "-\r\n" end + if (pi & 0xF000) == 0 then return "-\r\n" end set_rds_pi(pi) - return "+" + return "+\r\n" elseif cmd == "ecc" then local ecc = tonumber(value, 16) - if not ecc then return "-" end + if not ecc then return "-\r\n" end set_rds_ecc(ecc) - return "+" + return "+\r\n" elseif cmd == "pty" then local pty = tonumber(value) - if not pty then return "-" end + if not pty then return "-\r\n" end set_rds_pty(pty) - return "+" + return "+\r\n" elseif cmd == "slcd" then local slc_data = tonumber(value, 16) - if not slc_data then return "-" end + if not slc_data then return "-\r\n" end set_rds_slc_data(slc_data) - return "+" + return "+\r\n" elseif cmd == "ct" then local ct = tonumber(value) - if not ct then return "-" end + if not ct then return "-\r\n" end set_rds_ct(ct ~= 0) - return "+" + return "+\r\n" elseif cmd == "dpty" then local dpty = tonumber(value) - if not dpty then return "-" end + if not dpty then return "-\r\n" end set_rds_dpty(dpty ~= 0) - return "+" + return "+\r\n" elseif cmd == "tp" then local tp = tonumber(value) - if not tp then return "-" end + if not tp then return "-\r\n" end set_rds_tp(tp ~= 0) - return "+" + return "+\r\n" elseif cmd == "ta" then local ta = tonumber(value) - if not ta then return "-" end + if not ta then return "-\r\n" end set_rds_ta(ta ~= 0) - return "+" + return "+\r\n" elseif cmd == "rt1en" then local en = tonumber(value) - if not en then return "-" end + if not en then return "-\r\n" end set_rds_rt1_enabled(en ~= 0) - return "+" + return "+\r\n" elseif cmd == "rt2en" then local en = tonumber(value) - if not en then return "-" end + if not en then return "-\r\n" end set_rds_rt2_enabled(en ~= 0) - return "+" + return "+\r\n" elseif cmd == "ptynen" then local en = tonumber(value) - if not en then return "-" end + if not en then return "-\r\n" end set_rds_ptyn_enabled(en ~= 0) - return "+" + return "+\r\n" elseif cmd == "rttype" then local type = tonumber(value) - if not type then return "-" end + if not type then return "-\r\n" end set_rds_rt_type(type) - return "+" + return "+\r\n" elseif cmd == "rds2mod" then local type = tonumber(value) - if not type then return "-" end - set_rds2_mode(type ~= 0) - return "+" + if not type then return "-\r\n" end + set_rds2_mode(type) + return "+\r\n" elseif cmd == "rdsgen" then local type = tonumber(value) - if not type then return "-" end + if not type then return "-\r\n" end set_rds_streams(type) - return "+" + return "+\r\n" elseif cmd == "ptyn" then set_rds_ptyn(value) - return "+" + return "+\r\n" elseif cmd == "ps" then set_rds_ps(value) - return "+" + return "+\r\n" elseif cmd == "tps" then set_rds_tps(value) - return "+" + return "+\r\n" elseif cmd == "rt1" or cmd == "text" then set_rds_rt1(value) - return "+" + return "+\r\n" elseif cmd == "rt2" then set_rds_rt2(value) - return "+" + return "+\r\n" elseif cmd == "lps" then set_rds_lps(value) - return "+" + return "+\r\n" elseif cmd == "ert" then set_rds_ert(value) - return "+" + return "+\r\n" elseif cmd == "link" then local link = tonumber(value) - if not link then return "-" end + if not link then return "-\r\n" end set_rds_link(link ~= 0) - return "+" + return "+\r\n" elseif cmd == "rtper" then local period = tonumber(value) - if not period then return "-" end + if not period then return "-\r\n" end set_rds_rt_switching_period(period*60) - return "+" + return "+\r\n" elseif cmd == "program" then local program = tonumber(value) - if not program then return "-" end - if program < 1 or program > max_programs then return "-" end + if not program then return "-\r\n" end + if program < 1 or program > max_programs then return "-\r\n" end set_rds_program(program-1) set_rds_ta(false) - return "+" + return "+\r\n" elseif cmd == "level" then local level = tonumber(value) - if not level then return "-" end + if not level then return "-\r\n" end set_rds_level(level/255) - return "+" + return "+\r\n" elseif cmd == "dttmout" then local timeout = tonumber(value) - if not timeout then return "-" end + if not timeout then return "-\r\n" end set_rds_rt_text_timeout(timeout*60) - return "+" + return "+\r\n" elseif cmd == "grpseq" then set_rds_grpseq(value) - return "+" + return "+\r\n" elseif cmd == "grpseq2" then set_rds_grpseq2(value) - return "+" + return "+\r\n" elseif cmd == "rtp" or cmd == "ertp" then local is_ertp = (cmd == "ertp") local t1, s1, l1, t2, s2, l2 = value:match("(%d+),(%d+),(%d+),(%d+),(%d+),(%d+)") - if not l2 then return "-" end + if not l2 then return "-\r\n" end set_rds_rtplus_tags( is_ertp, ---@diagnostic disable-next-line: param-type-mismatch tonumber(t1), tonumber(s1), tonumber(l1), tonumber(t2), tonumber(s2), tonumber(l2) ) - return "+" + return "+\r\n" elseif cmd == "g" then local a, b, c, d = value:match("^(%x%x%x%x)(%x%x%x%x)(%x%x%x%x)(%x%x%x%x)$") if a and b and c and d then put_rds2_custom_group(tonumber(a, 16), tonumber(b, 16), tonumber(c, 16), tonumber(d, 16)) - return "+" + return "+\r\n" end local b1, c1, d1 = value:match("^(%x%x%x%x)(%x%x%x%x)(%x%x%x%x)$") if b1 and c1 and d1 then put_rds_custom_group(tonumber(b1, 16), tonumber(c1, 16), tonumber(d1, 16)) - return "+" + return "+\r\n" end - return "-" + return "-\r\n" elseif cmd == "rtprun" or cmd == "ertprun" then local is_ertp = (cmd == "ertprun") local f1_str, f2_str = value:match("([^,]+),?([^,]*)") - if not f1_str then return "-" end + if not f1_str then return "-\r\n" end local f1 = tonumber(f1_str) or 0 local f2 = tonumber(f2_str) or 0 @@ -327,44 +327,44 @@ function data_handle(data) set_rds_rtp_meta(is_ertp, running) if f2 ~= 0 then toggle_rds_rtp(is_ertp) end - return "+" + return "+\r\n" elseif cmd == "af" then local af_table = {} if value == "" or value == "0" then set_rds_af_group0({}) - return "+" + return "+\r\n" end for freq_str in value:gmatch("([^,]+)") do local f = tonumber(freq_str) if f then table.insert(af_table, f) - else return "-" end + else return "-\r\n" end end - if #af_table > 25 then return "-" end + if #af_table > 25 then return "-\r\n" end set_rds_af_group0(af_table) - return "+" + return "+\r\n" elseif cmd == "afo" then local af_table = {} if value == "" or value == "0" then set_rds_af_oda({}) - return "+" + return "+\r\n" end for freq_str in value:gmatch("([^,]+)") do local f = tonumber(freq_str) if f then table.insert(af_table, f) - else return "-" end + else return "-\r\n" end end - if #af_table > 25 then return "-" end + if #af_table > 25 then return "-\r\n" end set_rds_af_oda(af_table) - return "+" + return "+\r\n" else - return "?" + return "?\r\n" end end \ No newline at end of file diff --git a/src/lua_rds.c b/src/lua_rds.c index 68339f0..d1e84ea 100644 --- a/src/lua_rds.c +++ b/src/lua_rds.c @@ -177,12 +177,11 @@ INT_SETTER(rt_type) INT_GETTER(rt_type) int lua_set_rds2_mode(lua_State *localL) { - if (!lua_isboolean(localL, 1)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 1)); - mod->enc->encoder_data.rds2_mode = lua_toboolean(localL, 1); + mod->enc->encoder_data.rds2_mode = luaL_checkinteger(localL, 1); return 0; } int lua_get_rds2_mode(lua_State *localL) { - lua_pushboolean(localL, mod->enc->encoder_data.rds2_mode); + lua_pushinteger(localL, mod->enc->encoder_data.rds2_mode); return 1; } int lua_set_rds_streams(lua_State *localL) { @@ -601,10 +600,7 @@ int lua_group(RDSGroup* group, const char grp) { if (lua_isfunction(L, -1)) { lua_pushstring(L, &grp); - lua_pushinteger(L, group->b); - lua_pushinteger(L, group->c); - lua_pushinteger(L, group->d); - if (lua_pcall(L, 4, 4, 0) == LUA_OK) { + if (lua_pcall(L, 1, 4, 0) == LUA_OK) { if (!lua_isboolean(L, -1)) { pthread_mutex_unlock(&lua_mutex); return 0; @@ -636,6 +632,41 @@ int lua_group(RDSGroup* group, const char grp) { return 1; } +int lua_rds2_group(RDSGroup* group, int stream) { + pthread_mutex_lock(&lua_mutex); + lua_getglobal(L, "rds2_group"); + + if (lua_isfunction(L, -1)) { + lua_pushinteger(L, stream); + if (lua_pcall(L, 1, 4, 0) == LUA_OK) { + if (!lua_isinteger(L, -1)) { + pthread_mutex_unlock(&lua_mutex); + return 0; + } + if (!lua_isinteger(L, -2)) { + pthread_mutex_unlock(&lua_mutex); + return 0; + } + if (!lua_isinteger(L, -3)) { + pthread_mutex_unlock(&lua_mutex); + return 0; + } + if (!lua_isinteger(L, -4)) { + pthread_mutex_unlock(&lua_mutex); + return 0; + } + group->d = luaL_checkinteger(L, -1); + group->c = luaL_checkinteger(L, -2); + group->b = luaL_checkinteger(L, -3); + group->a = luaL_checkinteger(L, -4); + lua_pop(L, 3); + } else fprintf(stderr, "Lua error: %s at 'rds2_group'\n", lua_tostring(L, -1)); + lua_pop(L, 1); + } else lua_pop(L, 1); + pthread_mutex_unlock(&lua_mutex); + return 1; +} + void lua_group_ref(RDSGroup* group, int ref) { pthread_mutex_lock(&lua_mutex); lua_rawgeti(L, LUA_REGISTRYINDEX, ref); diff --git a/src/lua_rds.h b/src/lua_rds.h index 0ad5923..e839ae4 100644 --- a/src/lua_rds.h +++ b/src/lua_rds.h @@ -9,6 +9,7 @@ void init_lua(RDSModulator* rds_mod); void run_lua(char *str, char *cmd_output); int lua_group(RDSGroup* group, const char grp); +int lua_rds2_group(RDSGroup* group, int stream); void lua_call_function_nolock(const char* function); void lua_call_function(const char* function); void lua_group_ref(RDSGroup* group, int ref); diff --git a/src/rds.c b/src/rds.c index 642d380..734c6f4 100644 --- a/src/rds.c +++ b/src/rds.c @@ -245,6 +245,10 @@ static void get_rds_group(RDSEncoder* enc, RDSGroup *group, uint8_t stream) { get_rds_sequence_group(enc, group, &grp, stream); + goto group_coded_rds2; + } else if(enc->encoder_data.rds2_mode == 2) { + lua_rds2_group(group, stream); + if(group->a == 0) group->is_type_b = (IS_TYPE_B(group->b) != 0); goto group_coded_rds2; } } diff --git a/src/rds.h b/src/rds.h index 6cdbfff..601005e 100644 --- a/src/rds.h +++ b/src/rds.h @@ -1,6 +1,6 @@ #pragma once #include "common.h" -#define LUA_USER_DATA 1024 +#define LUA_USER_DATA 1280 /* The RDS error-detection code generator polynomial is * x^10 + x^8 + x^7 + x^5 + x^4 + x^3 + x^0 @@ -183,8 +183,7 @@ typedef struct { } RDSState; typedef struct { - uint16_t special_features; - uint8_t rds2_mode : 1; + uint8_t rds2_mode : 2; // uint8_t rds2_buffer[16384]; } RDSEncoderData; typedef struct {