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
+93
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
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
+2 -2
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
+2 -2
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