From 8c7a4d3e31618b465758ee20097ac38fe0b3a324 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Tue, 28 Apr 2026 15:40:26 +0200 Subject: [PATCH] bug fixes --- plugin.lua | 4 ++-- scripts/0-rds2_oda.lua | 25 +++++++++++++++++-------- scripts/1-oda.lua | 10 +++++----- src/lua_rds.c | 5 ++++- src/lua_rds.h | 2 +- src/rds.c | 6 +++--- src/rds95.c | 5 ++++- 7 files changed, 36 insertions(+), 21 deletions(-) diff --git a/plugin.lua b/plugin.lua index 4e22211..4f7979d 100644 --- a/plugin.lua +++ b/plugin.lua @@ -62,13 +62,13 @@ hooks.on_start = {} ---@type function[] hooks.on_state = {} ----This function is called every second +---This function is called every second (or rather if the second has changed since the last stream 0 group) ---It should be defined by the user in the script. ---This is a table of functions. Each will be called ---@type function[] hooks.tick = {} ----This function is called every minute +---This function is called every minute (or rather if the minute has changed since the last stream 0 group) ---It should be defined by the user in the script. ---This is a table of functions. Each will be called ---@type function[] diff --git a/scripts/0-rds2_oda.lua b/scripts/0-rds2_oda.lua index ab34353..5326258 100644 --- a/scripts/0-rds2_oda.lua +++ b/scripts/0-rds2_oda.lua @@ -1,7 +1,8 @@ -_RDS2_ODA = { aid = 0, data = 0, handler = false, file_related = false } +_RDS2_ODA = { aid = 0, data = 0, handler = false, file_related = false, channel = 0 } +_RDS2_next_channel = { normal = 16, file = 0 } -function _RDS2_ODA.new(aid, data, handler, file_related) - local instance = { aid = aid or 0, data = data or 0, handler = handler or false, file_related = file_related or false } +function _RDS2_ODA.new(aid, data, handler, file_related, channel) + local instance = { aid = aid or 0, data = data or 0, handler = handler or false, file_related = file_related or false, channel = channel or 0 } setmetatable(instance, { __index = _RDS2_ODA }) return instance end @@ -15,7 +16,16 @@ _RDS2_ODA_pointer = 1 ---@param file_related boolean ---@return integer oda_id function ext.register_oda_rds2(aid, data, file_related) - local oda = _RDS2_ODA.new(aid, data, false, file_related) + local channel + if file_related then + channel = _RDS2_next_channel.file + _RDS2_next_channel.file = _RDS2_next_channel.file + 1 + else + channel = _RDS2_next_channel.normal + _RDS2_next_channel.normal = _RDS2_next_channel.normal + 1 + end + + local oda = _RDS2_ODA.new(aid, data, false, file_related, channel) for i = 1, #_RDS2_ODAs do if _RDS2_ODAs[i] == false then _RDS2_ODAs[i] = oda @@ -64,8 +74,7 @@ function hooks.rds2_group(stream) if checked == #_RDS2_ODAs then return false, 0, 0, 0, 0 end local oda = _RDS2_ODAs[_RDS2_ODA_pointer] - local channel_offset = 16 * ((not oda.file_related) and 1 or 0) -- Channels 0-15 are reserved for file related things - local channel = ((_RDS2_ODA_pointer - 1 + channel_offset) & 0x3F) + local channel = oda.channel if oda.file_related then channel = channel & 0xF end _RDS2_ODA_pointer = _RDS2_ODA_pointer + 1 @@ -97,10 +106,10 @@ function hooks.rds2_group(stream) _RDS2_ODA_aid = _RDS2_ODA_aid + 1 if _RDS2_ODA_aid > 8 then _RDS2_ODA_aid = 0 end if oda.handler then - local generated = false + generated = false checked = 0 while generated == false and checked < #_RDS2_ODAs do - local ok, generated, a, b, c, d = pcall(oda.handler, stream) + ok, generated, a, b, c, d = pcall(oda.handler, stream) if not (generated and ok) then _RDS2_ODA_pointer = _RDS2_ODA_pointer + 1 if _RDS2_ODA_pointer > #_RDS2_ODAs then _RDS2_ODA_pointer = 1 end diff --git a/scripts/1-oda.lua b/scripts/1-oda.lua index a32bf1b..4fb7989 100644 --- a/scripts/1-oda.lua +++ b/scripts/1-oda.lua @@ -9,7 +9,7 @@ end local _RDS_ODAs = {} local _RDS_ODA_pointer = 1 ----Registers an ODA to be used in the O of the group sequence. ODAs are stored as state data, thus running reset_rds will clear it +---Registers an ODA to be used in the '\x06' of the group sequence. ODAs are stored as state data, thus running reset_rds will clear it ---Groups 14, 15, 2, 0 cannot be registered either version, groups 10, 4, 1 can be only registered as B, any other is free to take ---Group 3A will mean that there will be no group handler for this ODA, meaning it can only be interacted with via the 3A AID group, handler set is not possible with such groups ---@param group integer @@ -52,7 +52,7 @@ function ext.set_oda_id_data(oda_id, data) end ---Sets a function to handle the ODA data generation. ----The handler is called when the group sequence 'K' slot is processed. +---The handler is called when the group sequence '\xff' slot is processed. ---The function must return 3 integers representing RDS Blocks B, C, and D. ---Please note that you do not need to compute the block A to indentify the group and group version, that will be done for you and EVERY SINGLE group has PTY and TP inserted (and also PI if its a B) ---You are asked to set groups B last 5 bits, leave rest 0 @@ -74,9 +74,8 @@ local function get_aid() local b = 3 << 12 | oda.group << 1 | (oda.group_version and 1 or 0) local data, aid = oda.data, oda.aid - _RDS_ODA_pointer = (_RDS_ODA_pointer % #_RDS_ODAs) + 1 - if oda.temp then _RDS_ODAs[_RDS_ODA_pointer] = false end + _RDS_ODA_pointer = (_RDS_ODA_pointer % #_RDS_ODAs) + 1 return true, b, data, aid end @@ -117,7 +116,8 @@ local function group_handler(group_type) if _RDS_ODA_pointer > #_RDS_ODAs or _RDS_ODA_pointer < 1 then _RDS_ODA_pointer = 1 end if group_type == "\x06" then return get_aid() - elseif group_type == "\xff" then return get_data() end + elseif group_type == "\xff" then return get_data() + else return false, 0, 0, 0 end end rds.ext.register_group("\x06\xff", group_handler) diff --git a/src/lua_rds.c b/src/lua_rds.c index c5a36ae..2d48b86 100644 --- a/src/lua_rds.c +++ b/src/lua_rds.c @@ -7,11 +7,12 @@ lua_State *L = NULL; static pthread_mutex_t lua_mutex; #define lua_registertotable(L,n,f) (lua_pushcfunction(L, (f)), lua_setfield(L, -2, (n))) -void init_lua(RDSEncoder* _enc) { +uint8_t init_lua(RDSEncoder* _enc) { static int mutex_initialized = 0; enc = _enc; L = luaL_newstate(); printf("Initializing %s\n", LUA_COPYRIGHT); + if(L == NULL) return 0; luaL_requiref(L, "_G", luaopen_base, 1); luaL_requiref(L, LUA_STRLIBNAME, luaopen_string, 1); @@ -162,6 +163,8 @@ void init_lua(RDSEncoder* _enc) { pthread_mutex_init(&lua_mutex, NULL); mutex_initialized = 1; } + + return 1; } void run_lua(char *str, size_t str_len, char *cmd_output, size_t *out_len) { diff --git a/src/lua_rds.h b/src/lua_rds.h index c596cff..9b744a2 100644 --- a/src/lua_rds.h +++ b/src/lua_rds.h @@ -5,7 +5,7 @@ #include #include -void init_lua(RDSEncoder* _enc); +uint8_t init_lua(RDSEncoder* _enc); void run_lua(char *str, size_t str_len, char *cmd_output, size_t* out_len); int lua_group(RDSGroup* group, const char grp); int lua_rds2_group(RDSGroup* group, int stream); diff --git a/src/rds.c b/src/rds.c index 24df250..6cb3dc4 100644 --- a/src/rds.c +++ b/src/rds.c @@ -97,16 +97,16 @@ void get_rds_group(RDSEncoder* enc, RDSGroup *group, uint8_t stream) { time(&now); utc = gmtime(&now); - if(utc->tm_sec != enc->state[enc->program].last_second) { + if(utc->tm_sec != enc->state[enc->program].last_second && stream == 0) { enc->state[enc->program].last_second = utc->tm_sec; lua_call_tfunction("tick"); } - if (utc->tm_min != enc->state[enc->program].last_minute) { + if (utc->tm_min != enc->state[enc->program].last_minute && stream == 0) { enc->state[enc->program].last_minute = utc->tm_min; lua_call_tfunction("minute_tick"); - if(enc->data[enc->program].ct && stream == 0) { + if(enc->data[enc->program].ct) { get_rdsp_ct_group(group, now); goto group_coded; } diff --git a/src/rds95.c b/src/rds95.c index 548828f..4469291 100644 --- a/src/rds95.c +++ b/src/rds95.c @@ -169,7 +169,10 @@ int main(int argc, char **argv) { RDSModulator rdsModulator = {0}; RDSEncoder rdsEncoder = {0}; - init_lua(&rdsEncoder); + if(init_lua(&rdsEncoder) == 1) { + fprintf(stderr, "Could not create lua state - not enough memory\n"); + goto exit; + } init_rds_modulator(&rdsModulator, &rdsEncoder, config.num_streams); init_rds_encoder(&rdsEncoder);