0
1
mirror of https://github.com/radio95-rnt/rds95.git synced 2026-02-26 20:33:53 +01:00
This commit is contained in:
2025-12-29 17:48:39 +01:00
parent 73c7ee055d
commit 0ff07a80ba
2 changed files with 129 additions and 158 deletions

View File

@@ -366,19 +366,4 @@ function register_oda_rds2(aid, data, file_related) end
---Unregisters an RDS 2 ODA, this stops the handler or AID being called/sent ---Unregisters an RDS 2 ODA, this stops the handler or AID being called/sent
---@param oda_id integer ---@param oda_id integer
function unregister_oda_rds2(oda_id) end function unregister_oda_rds2(oda_id) end
---This function is defined externally
---Loads the file into RFT and initializes it if needed, note that this needs RDS2 mode 2
---@param aid integer for station logo use 0xFF7F
---@param path string filesystem path on the os
---@param id integer mostly use 0 here
---@param crc integer|boolean false for disabled, true for mode 7, and an integer for any of the modes
---@param once boolean true means that this file will be sent once and then unregistered
---@return boolean interrupted
function send_rft_file(aid, path, id, crc, once) end
---Pauses or resumes the process of sending of the RFT file
---@param aid integer
---@param paused boolean
function set_rft_paused(aid, paused) end

View File

@@ -1,82 +1,105 @@
_Rft_oda_id = nil ---@class RftInstance
_Rft_file = "" ---@field oda_id integer|nil The internal ODA registration ID
_Rft_crc_data = "" ---@field file_data string The raw binary content of the file
_Rft_file_segment = 0 ---@field crc_data string Calculated CRC chunks for Variant 1 groups
_Rft_crc_segment = 0 ---@field file_segment integer Current segment index being sent
_Rft_toggle = false ---@field crc_segment integer Current CRC byte index being sent
_Rft_last_id = -1 ---@field toggle boolean The RDS2 toggle bit (flips when file content changes)
_Rft_version = 0 ---@field last_id integer The file ID used in the previous transmission
_Rft_crc = false ---@field version integer The file version (0-7)
_Rft_crc_full_file = 0 ---@field crc_enabled boolean Whether CRC protection is active
_Rft_crc_mode = 0 ---@field crc_full_file integer The CRC16 of the entire file (for mode 0)
_Rft_crc_sent = false ---@field crc_mode integer The RDS2 RFT CRC mode (0-5)
_Rft_aid = 0 ---@field crc_sent_this_cycle boolean Internal flag to interleave CRCs with data
_Rft_send_once = false ---@field aid integer The Application Identification (e.g., 0xFF7F for Logo)
_Rft_paused = false ---@field send_once boolean If true, unregisters after one full transmission
---@field paused boolean If true, the ODA handler will return empty groups
RftInstance = {}
RftInstance.__index = RftInstance
local function stop_rft() --- Creates a new RFT Manager instance.
if _Rft_oda_id ~= nil and _Rft_aid ~= 0 then ---@return RftInstance
unregister_oda_rds2(_Rft_oda_id) function RftInstance.new()
_Rft_oda_id = nil local self = setmetatable({}, RftInstance)
_Rft_aid = 0
end
_Rft_file = "" self.oda_id = nil
_Rft_crc_data = "" self.file_data = ""
_Rft_file_segment = 0 self.crc_data = ""
_Rft_crc_segment = 0 self.file_segment = 0
_Rft_toggle = false self.crc_segment = 1
_Rft_last_id = -1 self.toggle = false
_Rft_version = 0 self.last_id = -1
_Rft_crc = false self.version = 0
_Rft_crc_full_file = 0 self.crc_enabled = false
_Rft_crc_mode = 0 self.crc_full_file = 0
_Rft_crc_sent = false self.crc_mode = 0
_Rft_paused = false self.crc_sent_this_cycle = false
self.aid = 0
self.send_once = false
self.paused = false
return self
end end
local function start_rft() function RftInstance:stop()
if _Rft_oda_id == nil and _Rft_aid ~= 0 then if self.oda_id ~= nil and self.aid ~= 0 then
_Rft_oda_id = register_oda_rds2(_Rft_aid, 0, true) unregister_oda_rds2(self.oda_id)
set_oda_handler_rds2(_Rft_oda_id, function (stream) self.oda_id = nil
if #_Rft_file == 0 or _Rft_paused then return false, 0, 0, 0, 0 end end
local total_segments = math.ceil(#_Rft_file / 5) self.file_data = ""
local seg = _Rft_file_segment self.crc_data = ""
local base = seg * 5 + 1 self.file_segment = 0
self.crc_segment = 1
self.paused = false
end
if not _Rft_crc_sent and _Rft_crc and (seg % 16 == 0) and stream == 1 then --- Internal method to start the ODA handler logic.
_Rft_crc_sent = true --- Processes RDS2 RFT Variants 0, 1 and Data Groups.
local chunk_address = math.floor((_Rft_crc_segment - 1) / 2) ---@private
local c = (1 << 12) | (_Rft_crc_mode & 7) << 9 | (chunk_address & 0x1ff) function RftInstance:start()
if self.oda_id == nil and self.aid ~= 0 then
self.oda_id = register_oda_rds2(self.aid, 0, true)
local high_byte = 0 set_oda_handler_rds2(self.oda_id, function(stream)
local low_byte = 0 if #self.file_data == 0 or self.paused then
if _Rft_crc_mode ~= 0 then return false, 0, 0, 0, 0
high_byte = string.byte(_Rft_crc_data, _Rft_crc_segment) or 0 end
low_byte = string.byte(_Rft_crc_data, _Rft_crc_segment + 1) or 0
local total_segments = math.ceil(#self.file_data / 5)
if not self.crc_sent_this_cycle and self.crc_enabled and (self.file_segment % 16 == 0) and stream == 1 then
self.crc_sent_this_cycle = true
local chunk_address = math.floor((self.crc_segment - 1) / 2)
local c = (1 << 12) | (self.crc_mode & 7) << 9 | (chunk_address & 0x1ff)
local high_byte, low_byte
if self.crc_mode ~= 0 then
high_byte = string.byte(self.crc_data, self.crc_segment) or 0
low_byte = string.byte(self.crc_data, self.crc_segment + 1) or 0
else else
high_byte = _Rft_crc_full_file >> 8 high_byte = self.crc_full_file >> 8
low_byte = _Rft_crc_full_file & 0xff low_byte = self.crc_full_file & 0xff
end end
_Rft_crc_segment = _Rft_crc_segment + 2 self.crc_segment = self.crc_segment + 2
if _Rft_crc_segment > #_Rft_crc_data then _Rft_crc_segment = 1 end if self.crc_segment > #self.crc_data then self.crc_segment = 1 end
return true, (2 << 14), _Rft_aid, c, (high_byte << 8) | low_byte return true, (2 << 14), self.aid, c, (high_byte << 8) | low_byte
else _Rft_crc_sent = false end else self.crc_sent_this_cycle = false end
local function b(i) return string.byte(_Rft_file, base + i) or 0 end local base = self.file_segment * 5 + 1
local function b(i) return string.byte(self.file_data, base + i) or 0 end
local word1 = (((_Rft_toggle and 1 or 0) << 7) | ((seg >> 8) & 0x7F)) local word1 = (((self.toggle and 1 or 0) << 7) | ((self.file_segment >> 8) & 0x7F))
local word2 = ((seg & 0xFF) << 8) | b(0) local word2 = ((self.file_segment & 0xFF) << 8) | b(0)
local word3 = (b(1) << 8) | b(2) local word3 = (b(1) << 8) | b(2)
local word4 = (b(3) << 8) | b(4) local word4 = (b(3) << 8) | b(4)
_Rft_file_segment = seg + 1 self.file_segment = self.file_segment + 1
if _Rft_file_segment >= total_segments then if self.file_segment >= total_segments then
_Rft_file_segment = 0 self.file_segment = 0
if _Rft_send_once then stop_rft() end if self.send_once then self:stop() end
end end
return true, (2 << 12) | word1, word2, word3, word4 return true, (2 << 12) | word1, word2, word3, word4
@@ -84,99 +107,62 @@ local function start_rft()
end end
end end
---This function is defined externally --- Loads a file and begins RDS2 transmission.
---Loads the file into RFT and initializes it if needed, note that this needs RDS2 mode 2 ---@param aid integer Application ID (e.g. 0xFF7F for Station Logo)
---@param aid integer for station logo use 0xFF7F ---@param path string System path to the file
---@param path string filesystem path on the os ---@param id integer File ID (0-63). Use the same ID to update a file, different to trigger a reset.
---@param id integer mostly use 0 here ---@param crc integer|boolean CRC Mode (0: Full, 1-5: Chunks, true/7: Auto)
---@param crc integer|boolean false for disabled, true for mode 7, and an integer for any of the modes ---@param once boolean If true, file is sent once and the stream is closed
---@param once boolean true means that this file will be sent once and then unregistered ---@return boolean interrupted Returns true if a previous file was already being sent
---@return boolean interrupted function RftInstance:sendFile(aid, path, id, crc, once)
function send_rft_file(aid, path, id, crc, once) local interrupted = (#self.file_data ~= 0)
local interrupted = (#_Rft_file ~= 0)
if _Rft_aid ~= aid then stop_rft() end if self.aid ~= aid then self:stop() end
_Rft_aid = aid self.aid = aid
local file = io.open(path, "rb") local file = io.open(path, "rb")
if not file then error("Could not open file") end if not file then error("Could not open file: " .. path) end
_Rft_file = file:read("*a") self.file_data = file:read("*a")
file:close() file:close()
_Rft_send_once = once self.send_once = once
if id == _Rft_last_id then if id == self.last_id then
_Rft_toggle = not _Rft_toggle self.toggle = not self.toggle
_Rft_crc_sent = 0 self.version = (self.version + 1) % 8
_Rft_version = _Rft_version + 1
if _Rft_version > 7 then _Rft_version = 0 end
end end
_Rft_crc_data = "" self.crc_data = ""
_Rft_crc = (crc ~= false) self.crc_enabled = (crc ~= false)
local chunk_size = 0 local f_size = #self.file_data
if crc and crc == 0 then if crc == 0 then
_Rft_crc_mode = 0 self.crc_mode = 0
_Rft_crc_full_file = crc16(_Rft_file) self.crc_full_file = crc16(self.file_data)
elseif crc and crc == 1 and #_Rft_file <= 40960 then elseif crc == true or crc == 7 then
_Rft_crc_mode = 1 if f_size <= 40960 then self.crc_mode = 1
chunk_size = 5 * 16 elseif f_size > 40960 and f_size <= 81920 then self.crc_mode = 2
elseif crc and crc == 2 and #_Rft_file < 40960 and #_Rft_file >= 81920 then else self.crc_mode = 3 end
_Rft_crc_mode = 2
chunk_size = 5 * 32
elseif crc and crc == 3 and #_Rft_file > 81960 then
_Rft_crc_mode = 3
chunk_size = 5 * 64
elseif crc and crc == 4 and #_Rft_file > 81960 then
_Rft_crc_mode = 4
chunk_size = 5 * 128
elseif crc and crc == 5 and #_Rft_file > 81960 then
_Rft_crc_mode = 5
chunk_size = 5 * 256
elseif crc and (crc == 7 or crc == true) then
if #_Rft_file <= 40960 then
_Rft_crc_mode = 1
chunk_size = 5*16
elseif #_Rft_file > 40960 and #_Rft_file <= 81920 then
_Rft_crc_mode = 2
chunk_size = 5*32
elseif #_Rft_file > 81960 then
_Rft_crc_mode = 3
chunk_size = 5*64
end
else else
_Rft_crc = false self.crc_mode = crc and 1 or 0
end end
if _Rft_crc and chunk_size ~= 0 then local chunk_multipliers = {16, 32, 64, 128, 256}
for i = 1, #_Rft_file, chunk_size do local multiplier = chunk_multipliers[self.crc_mode]
local chunk = string.sub(_Rft_file, i, i + chunk_size - 1) if self.crc_enabled and multiplier then
local crc_val = crc16(chunk) local chunk_size = 5 * multiplier
_Rft_crc_data = _Rft_crc_data .. string.char(math.floor(crc_val / 256), crc_val % 256) for i = 1, f_size, chunk_size do
local chunk = string.sub(self.file_data, i, i + chunk_size - 1)
local v = crc16(chunk)
self.crc_data = self.crc_data .. string.char(v >> 8, v & 0xff)
end end
end end
if #_Rft_file > 262143 then error("The file is too large", 2) end if f_size > 262143 then error("File too large") end
if _Rft_oda_id == nil then start_rft() end if self.oda_id == nil then self:start() end
---@diagnostic disable-next-line: param-type-mismatch
set_oda_id_data_rds2(_Rft_oda_id, #_Rft_file | (id & 63) << 18 | (_Rft_version & 7) << 24 | (_Rft_crc and 1 or 0) << 27)
_Rft_last_id = id
_Rft_paused = false set_oda_id_data_rds2(self.oda_id, f_size | (id & 63) << 18 | (self.version & 7) << 24 | (self.crc_enabled and 1 or 0) << 27)
self.last_id = id
self.paused = false
return interrupted return interrupted
end end
---Pauses or resumes the process of sending of the RFT file
---@param aid integer
---@param paused boolean
function set_rft_paused(aid, paused)
if aid ~= _Rft_aid then error("AID does not match", 2) end
_Rft_paused = paused
end
local _old_on_state_oda_rft = on_state
function on_state()
stop_rft()
if type(_old_on_state_oda_rft) == "function" then _old_on_state_oda_rft() end
end