0
1
mirror of https://github.com/radio95-rnt/rds95.git synced 2026-02-26 12:32:05 +01:00

oda in lua?

This commit is contained in:
2025-12-27 11:41:26 +01:00
parent b39a0e78ed
commit 3a6c701157
10 changed files with 203 additions and 137 deletions

View File

@@ -58,6 +58,7 @@ function group(group) end
---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 boolean generated
---@return integer a
---@return integer b
---@return integer c
@@ -300,7 +301,7 @@ function register_oda(group, group_version, id, id_data) end
function set_oda_id_data(oda_id, data) end
---The callback function for an ODA handler
---@alias ODAHandler fun(): (integer, integer, integer)
---@alias ODAHandler fun(): (boolean, integer, integer, integer)
---Sets a function to handle the ODA data generation.
---The handler is called when the group sequence 'K' slot is processed.
@@ -327,4 +328,23 @@ function get_userdata() end
---@param offset integer
---@param size integer
---@return string
function get_userdata_offset(offset, size) end
function get_userdata_offset(offset, size) end
---The callback function for an ODA handler
---@alias RDS2_ODAHandler fun(): (boolean, integer, integer, integer, integer)
---This function is defined externally
---@param oda_id integer
---@param func RDS2_ODAHandler
function set_oda_handler_rds2(oda_id, func) end
---This function is defined externally
---@param oda_id integer
---@param data integer
function set_oda_id_data_rds2(oda_id, data) end
---This function is defined externally
---@param aid integer
---@param data integer
---@return integer oda_id
function register_oda_rds2(aid, data) end

93
scripts/0-oda.lua Normal file
View File

@@ -0,0 +1,93 @@
local ODA = { group = 0, group_version = false, aid = 0, data = 0, handler = false }
function ODA.new(group, group_version, aid, data, handler)
local instance = { group = group or 0, group_version = group_version or false, aid = aid or 0, data = data or 0, handler = handler or false }
setmetatable(instance, { __index = ODA })
return instance
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
---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
---@param group_version boolean
---@param aid integer
---@param data integer
---@return integer oda_id
function register_oda(group, group_version, aid, data)
if group == 14 or group == 15 or group == 2 or group == 0 then error("Group is incorrect", 2) end
if (group == 10 or group == 4 or group == 1) and group_version then error("Group is incorrect", 2) end
local oda = ODA.new(group, group_version, aid, data, false)
table.insert(RDS_ODAs, oda)
return #RDS_ODAs
end
---Sets the id_data for a existing ODA group
---@param oda_id integer
---@param data integer
function set_oda_id_data(oda_id, data)
if oda_id > #RDS_ODAs then return end
RDS_ODAs[oda_id].data = data
end
---Sets a function to handle the ODA data generation.
---The handler is called when the group sequence 'K' 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
---@param oda_id integer The ID returned by register_oda
---@param fun ODAHandler
function set_oda_handler(oda_id, fun)
if oda_id > #RDS_ODAs then return end
RDS_ODAs[oda_id].handler = fun
end
local function get_aid()
local oda = RDS_ODAs[RDS_ODA_pointer]
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
return b, data, aid
end
local function get_data()
local checked_count = 0
local total_odas = #RDS_ODAs
while checked_count < total_odas do
local oda = RDS_ODAs[RDS_ODA_pointer]
if type(oda.handler) == "function" then
local generated, b, c, d = oda.handler()
RDS_ODA_pointer = (RDS_ODA_pointer % #RDS_ODAs) + 1
b = b | oda.group << 12
b = b | (oda.group_version and 1 or 0) << 11
return generated, b, c, d
end
RDS_ODA_pointer = (RDS_ODA_pointer % #RDS_ODAs) + 1
checked_count = checked_count + 1
end
return false, 0, 0, 0
end
function group(group_type)
if group_type == "O" or group_type == "K" then
if #RDS_ODAs == 0 then return false, 0, 0, 0 end
if RDS_ODA_pointer > #RDS_ODAs or RDS_ODA_pointer < 1 then RDS_ODA_pointer = 1 end
if group_type == "O" then
local b, c, d = get_aid()
return true, b, c, d
elseif group_type == "K" then
return get_data()
end
end
return false, 0, 0, 0
end

57
scripts/0-rds2_oda.lua Normal file
View File

@@ -0,0 +1,57 @@
local RDS2_ODA = { aid = 0, data = 0, handler = false }
function RDS2_ODA.new(aid, data, handler)
local instance = { aid = aid or 0, data = data or 0, handler = handler or false }
setmetatable(instance, { __index = RDS2_ODA })
return instance
end
local RDS2_ODAs = {}
local RDS2_ODA_aid = true
local RDS2_ODA_pointer = 1
---This function is defined externally
---@param aid integer
---@param data integer
---@return integer oda_id
function register_oda_rds2(aid, data)
local oda = RDS2_ODA.new(aid, data, false)
table.insert(RDS2_ODAs, oda)
return #RDS2_ODAs
end
---This function is defined externally
---@param oda_id integer
---@param data integer
function set_oda_id_data_rds2(oda_id, data)
if oda_id > #RDS2_ODAs then return end
RDS2_ODAs[oda_id].data = data
end
---This function is defined externally
---@param oda_id integer
---@param func RDS2_ODAHandler
function set_oda_handler_rds2(oda_id, func)
if oda_id > #RDS2_ODAs then return end
RDS2_ODAs[oda_id].handler = func
end
function rds2_group(stream)
if #RDS2_ODAs == 0 then return false, 0, 0, 0, 0 end
if RDS2_ODA_pointer > #RDS2_ODAs then RDS2_ODA_pointer = 1 end
local oda = RDS2_ODAs[RDS2_ODA_pointer]
local channel = (RDS2_ODA_pointer & 0x40) << 8
RDS2_ODA_pointer = RDS2_ODA_pointer + 1
if RDS2_ODA_aid then
-- TODO: add support for the multi aid thing (page 49)
RDS2_ODA_aid = not RDS2_ODA_aid
return true, 1 << 15 | channel, oda.aid, (oda.data & 0xffff0000) >> 16, (oda.data & 0xffff)
else
RDS2_ODA_aid = not RDS2_ODA_aid
if oda.handler then
local generated, a, b, c, d = oda.handler(stream)
return generated, 1 << 14 | channel | a, b, c, d
end
return true, 1 << 15 | channel, oda.aid, (oda.data & 0xffff0000) >> 16, (oda.data & 0xffff)
end
end

View File

@@ -16,7 +16,7 @@ local function init_ert()
local segments = string.byte(get_userdata_offset(257, 1))
if segments == 0 then return 0, 0, 0 end
if segments == 0 then return false, 0, 0, 0 end
if _Ert_state >= segments then _Ert_state = 0 end
@@ -26,7 +26,7 @@ local function init_ert()
local d = (string.byte(chunk, 3) << 8) | string.byte(chunk, 4)
_Ert_state = (_Ert_state + 1) % segments
return b, c, d
return true, b, c, d
end)
end
end

View File

@@ -21,7 +21,7 @@ local function init_rtp()
d = d | (string.byte(data_1, 2) & 0x3f) << 5
d = d | (string.byte(data_1, 3) & 0x1f)
return b, c, d
return true, b, c, d
end)
end
end
@@ -44,7 +44,7 @@ local function init_ertp()
d = d | (string.byte(data_1, 2) & 0x3f) << 5
d = d | (string.byte(data_1, 3) & 0x1f)
return b, c, d
return true, b, c, d
end)
end
end

View File

@@ -402,56 +402,6 @@ int lua_set_rds_udg2(lua_State *localL) {
return 0;
}
int lua_register_oda(lua_State *localL) {
if (!lua_isboolean(localL, 2)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 2));
uint8_t id = mod->enc->state[mod->enc->program].user_oda.oda_len++;
if(mod->enc->state[mod->enc->program].user_oda.oda_len >= ODAS) return luaL_error(localL, "There can't be more than %d registered ODAs", ODAS);
if(mod->enc->state[mod->enc->program].user_oda.odas[id].group != 0) return luaL_error(localL, "internal error");
mod->enc->state[mod->enc->program].user_oda.odas[id].group = luaL_checkinteger(localL, 1);
switch (mod->enc->state[mod->enc->program].user_oda.odas[id].group) {
case 14:
case 15:
case 2:
case 0:
return luaL_error(localL, "Invalid group");
break;
case 10:
case 4:
case 1:
if(mod->enc->state[mod->enc->program].user_oda.odas[id].group_version == 0) return luaL_error(localL, "Invalid group");
default:
break;
}
mod->enc->state[mod->enc->program].user_oda.odas[id].group_version = lua_toboolean(localL, 2);
mod->enc->state[mod->enc->program].user_oda.odas[id].id = luaL_checkinteger(localL, 3);
mod->enc->state[mod->enc->program].user_oda.odas[id].id_data = luaL_checkinteger(localL, 4);
lua_pushinteger(localL, id);
return 1;
}
int lua_set_oda_id_data(lua_State *localL) {
uint8_t idx = luaL_checkinteger(localL, 1);
if(idx >= ODAS) return luaL_error(localL, "There can't be more than %d registered ODAs", ODAS);
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].group == 0) return luaL_error(localL, "this oda is not registered");
mod->enc->state[mod->enc->program].user_oda.odas[idx].id_data = luaL_checkinteger(localL, 2);
return 0;
}
int lua_set_oda_handler(lua_State *localL) {
uint8_t idx = luaL_checkinteger(localL, 1);
if(idx >= ODAS) return luaL_error(localL, "There can't be more than %d registered ODAs", ODAS);
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].group == 0) return luaL_error(localL, "this oda is not registered");
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].group == 3) return luaL_error(localL, "this oda cannot have a handler");
luaL_checktype(localL, 2, LUA_TFUNCTION);
lua_pushvalue(localL, 2);
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler != 0) luaL_unref(localL, LUA_REGISTRYINDEX, mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler);
mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler = luaL_ref(localL, LUA_REGISTRYINDEX);
int index = *unload_refs;
unload_refs[index] = mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler;
(*unload_refs)++;
return 0;
}
void init_lua(RDSModulator* rds_mod) {
static int mutex_initialized = 0;
mod = rds_mod;
@@ -474,8 +424,6 @@ void init_lua(RDSModulator* rds_mod) {
lua_setglobal(L, "eon_count");
lua_pushinteger(L, LUA_USER_DATA);
lua_setglobal(L, "user_data_len");
lua_pushinteger(L, ODAS);
lua_setglobal(L, "oda_count");
lua_register(L, "set_rds_program_defaults", lua_set_rds_program_defaults);
lua_register(L, "reset_rds", lua_reset_rds);
@@ -566,10 +514,6 @@ void init_lua(RDSModulator* rds_mod) {
lua_register(L, "set_rds_udg", lua_set_rds_udg);
lua_register(L, "set_rds_udg2", lua_set_rds_udg2);
lua_register(L, "register_oda", lua_register_oda);
lua_register(L, "set_oda_id_data", lua_set_oda_id_data);
lua_register(L, "set_oda_handler", lua_set_oda_handler);
lua_register(L, "set_userdata", lua_set_userdata);
lua_register(L, "set_userdata_offset", lua_set_userdata_offset);
lua_register(L, "get_userdata", lua_get_userdata);
@@ -649,8 +593,8 @@ int lua_rds2_group(RDSGroup* group, int stream) {
if (lua_isfunction(L, -1)) {
lua_pushinteger(L, stream);
if (lua_pcall(L, 1, 4, 0) == LUA_OK) {
if (!lua_isinteger(L, -1)) {
if (lua_pcall(L, 1, 5, 0) == LUA_OK) {
if (!lua_isboolean(L, -1)) {
pthread_mutex_unlock(&lua_mutex);
return 0;
}
@@ -666,11 +610,19 @@ int lua_rds2_group(RDSGroup* group, int stream) {
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);
if (!lua_isinteger(L, -5)) {
pthread_mutex_unlock(&lua_mutex);
return 0;
}
if(lua_toboolean(L, -1) == 0) {
pthread_mutex_unlock(&lua_mutex);
return 0;
}
group->d = luaL_checkinteger(L, -2);
group->c = luaL_checkinteger(L, -3);
group->b = luaL_checkinteger(L, -4);
group->a = luaL_checkinteger(L, -5);
lua_pop(L, 4);
} else fprintf(stderr, "Lua error: %s at 'rds2_group'\n", lua_tostring(L, -1));
lua_pop(L, 1);
} else lua_pop(L, 1);

View File

@@ -91,16 +91,12 @@ static void get_rds_sequence_group(RDSEncoder* enc, RDSGroup *group, char* grp,
get_rds_fasttuning_group(enc, group);
break;
case 'L':
if(get_rdsp_lua_group(group, *grp) == 0) get_rds_ps_group(enc, group);
break;
case 'O':
get_rds_user_oda_group(enc, group);
break;
case 'R':
case 'P':
case 'S':
case 'O':
case 'K':
if(get_rds_user_oda_group_content(enc, group) == 0) get_rds_ps_group(enc, group);
if(get_rdsp_lua_group(group, *grp) == 0) get_rds_ps_group(enc, group);
break;
case 'U':
if(enc->state[enc->program].af_oda == 0) get_rds_oda_af_group(enc, group);
@@ -128,16 +124,7 @@ static uint8_t check_rds_good_group(RDSEncoder* enc, char* grp) {
if(*grp == 'Y' && enc->data[enc->program].udg2_len != 0) good_group = 1;
if(*grp == 'F' && enc->data[enc->program].lps[0] != '\0') good_group = 1;
if(*grp == 'T') good_group = 1;
if(*grp == 'L') good_group = 1;
if(*grp == 'O' && enc->state[enc->program].user_oda.oda_len != 0) good_group = 1;
if(*grp == 'K') {
for (int i = 0; i < enc->state->user_oda.oda_len; i++) {
if (enc->state->user_oda.odas[i].lua_handler != 0) {
good_group = 1;
break;
}
}
}
if(*grp == 'L' || *grp = 'R' || *grp = 'P' || *grp = 'S' || *grp = 'O' || *grp = 'K') good_group = 1;
if(*grp == 'U' && enc->data[enc->program].af_oda.num_afs) good_group = 1;
return good_group;
}
@@ -247,8 +234,14 @@ static void get_rds_group(RDSEncoder* enc, RDSGroup *group, uint8_t 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);
int generated = lua_rds2_group(group, stream);
if(group->a == 0 && generated) group->is_type_b = (IS_TYPE_B(group->b) != 0);
else if(generated == 0) {
group->b = enc->state[enc->program].last_stream0_group[0];
group->c = enc->state[enc->program].last_stream0_group[1];
group->d = enc->state[enc->program].last_stream0_group[2];
group->is_type_b = enc->state[enc->program].last_stream0_group_type_b;
}
goto group_coded_rds2;
}
}

View File

@@ -1,7 +1,6 @@
#pragma once
#include "common.h"
#define LUA_USER_DATA 1280
#define ODAS 32
#define LUA_USER_DATA 1152
/* The RDS error-detection code generator polynomial is
* x^10 + x^8 + x^7 + x^5 + x^4 + x^3 + x^0
@@ -108,20 +107,6 @@ typedef struct {
uint8_t af_state : 6;
} RDSEONState;
typedef struct {
uint8_t group : 4;
uint8_t group_version : 1;
uint16_t id;
uint16_t id_data;
int lua_handler;
} RDSODA;
typedef struct {
uint8_t oda_len;
uint8_t oda_pointer;
uint8_t oda_runner_pointer;
RDSODA odas[ODAS];
} RDSODAState;
typedef struct {
uint8_t ps_update : 1;
uint8_t tps_update : 1;
@@ -179,8 +164,6 @@ typedef struct {
uint16_t last_stream0_group[3];
uint8_t last_stream0_group_type_b : 1;
RDSODAState user_oda;
} RDSState;
typedef struct {

View File

@@ -311,36 +311,4 @@ int get_rdsp_lua_group(RDSGroup *group, const char grp) {
int generated = lua_group(group, grp);
if(generated) group->is_type_b = (IS_TYPE_B(group->b) != 0);
return generated;
}
void get_rds_user_oda_group(RDSEncoder* enc, RDSGroup *group) {
uint8_t pointer = enc->state[enc->program].user_oda.oda_pointer++;
if(enc->state[enc->program].user_oda.oda_pointer >= enc->state[enc->program].user_oda.oda_len) enc->state[enc->program].user_oda.oda_pointer = 0;
RDSODA oda = enc->state[enc->program].user_oda.odas[pointer];
group->b |= 3 << 12;
group->b |= oda.group << 1;
group->b |= oda.group_version;
group->c = oda.id_data;
group->d = oda.id;
}
int get_rds_user_oda_group_content(RDSEncoder* enc, RDSGroup *group) {
RDSODAState *oda_state = &enc->state[enc->program].user_oda;
if (oda_state->oda_len == 0) return 0;
for (uint8_t i = 0; i < oda_state->oda_len; i++) {
uint8_t current_idx = oda_state->oda_runner_pointer;
oda_state->oda_runner_pointer = (oda_state->oda_runner_pointer + 1) % oda_state->oda_len;
if (oda_state->odas[current_idx].lua_handler != 0 && oda_state->odas[current_idx].group != 3) {
lua_group_ref(group, oda_state->odas[current_idx].lua_handler);
group->b |= oda_state->odas[current_idx].group << 12;
group->b |= oda_state->odas[current_idx].group_version << 11;
group->is_type_b = (IS_TYPE_B(group->b) != 0);
return 1;
}
}
return 0;
}