diff --git a/plugin.lua b/plugin.lua index b34fda2..fb4304f 100644 --- a/plugin.lua +++ b/plugin.lua @@ -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 ---@param oda_id integer -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 \ No newline at end of file +function unregister_oda_rds2(oda_id) end \ No newline at end of file diff --git a/scripts/1-rft.lua b/scripts/1-rft.lua index e94b16b..f2eb0df 100644 --- a/scripts/1-rft.lua +++ b/scripts/1-rft.lua @@ -1,82 +1,105 @@ -_Rft_oda_id = nil -_Rft_file = "" -_Rft_crc_data = "" -_Rft_file_segment = 0 -_Rft_crc_segment = 0 -_Rft_toggle = false -_Rft_last_id = -1 -_Rft_version = 0 -_Rft_crc = false -_Rft_crc_full_file = 0 -_Rft_crc_mode = 0 -_Rft_crc_sent = false -_Rft_aid = 0 -_Rft_send_once = false -_Rft_paused = false +---@class RftInstance +---@field oda_id integer|nil The internal ODA registration ID +---@field file_data string The raw binary content of the file +---@field crc_data string Calculated CRC chunks for Variant 1 groups +---@field file_segment integer Current segment index being sent +---@field crc_segment integer Current CRC byte index being sent +---@field toggle boolean The RDS2 toggle bit (flips when file content changes) +---@field last_id integer The file ID used in the previous transmission +---@field version integer The file version (0-7) +---@field crc_enabled boolean Whether CRC protection is active +---@field crc_full_file integer The CRC16 of the entire file (for mode 0) +---@field crc_mode integer The RDS2 RFT CRC mode (0-5) +---@field crc_sent_this_cycle boolean Internal flag to interleave CRCs with data +---@field aid integer The Application Identification (e.g., 0xFF7F for Logo) +---@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() - if _Rft_oda_id ~= nil and _Rft_aid ~= 0 then - unregister_oda_rds2(_Rft_oda_id) - _Rft_oda_id = nil - _Rft_aid = 0 - end +--- Creates a new RFT Manager instance. +---@return RftInstance +function RftInstance.new() + local self = setmetatable({}, RftInstance) - _Rft_file = "" - _Rft_crc_data = "" - _Rft_file_segment = 0 - _Rft_crc_segment = 0 - _Rft_toggle = false - _Rft_last_id = -1 - _Rft_version = 0 - _Rft_crc = false - _Rft_crc_full_file = 0 - _Rft_crc_mode = 0 - _Rft_crc_sent = false - _Rft_paused = false + self.oda_id = nil + self.file_data = "" + self.crc_data = "" + self.file_segment = 0 + self.crc_segment = 1 + self.toggle = false + self.last_id = -1 + self.version = 0 + self.crc_enabled = false + self.crc_full_file = 0 + self.crc_mode = 0 + self.crc_sent_this_cycle = false + self.aid = 0 + self.send_once = false + self.paused = false + + return self end -local function start_rft() - if _Rft_oda_id == nil and _Rft_aid ~= 0 then - _Rft_oda_id = register_oda_rds2(_Rft_aid, 0, true) - set_oda_handler_rds2(_Rft_oda_id, function (stream) - if #_Rft_file == 0 or _Rft_paused then return false, 0, 0, 0, 0 end +function RftInstance:stop() + if self.oda_id ~= nil and self.aid ~= 0 then + unregister_oda_rds2(self.oda_id) + self.oda_id = nil + end - local total_segments = math.ceil(#_Rft_file / 5) - local seg = _Rft_file_segment - local base = seg * 5 + 1 + self.file_data = "" + self.crc_data = "" + 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 - _Rft_crc_sent = true - local chunk_address = math.floor((_Rft_crc_segment - 1) / 2) - local c = (1 << 12) | (_Rft_crc_mode & 7) << 9 | (chunk_address & 0x1ff) +--- Internal method to start the ODA handler logic. +--- Processes RDS2 RFT Variants 0, 1 and Data Groups. +---@private +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 - local low_byte = 0 - if _Rft_crc_mode ~= 0 then - high_byte = string.byte(_Rft_crc_data, _Rft_crc_segment) or 0 - low_byte = string.byte(_Rft_crc_data, _Rft_crc_segment + 1) or 0 + set_oda_handler_rds2(self.oda_id, function(stream) + if #self.file_data == 0 or self.paused then + return false, 0, 0, 0, 0 + end + + 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 - high_byte = _Rft_crc_full_file >> 8 - low_byte = _Rft_crc_full_file & 0xff + high_byte = self.crc_full_file >> 8 + low_byte = self.crc_full_file & 0xff end - _Rft_crc_segment = _Rft_crc_segment + 2 - if _Rft_crc_segment > #_Rft_crc_data then _Rft_crc_segment = 1 end + self.crc_segment = self.crc_segment + 2 + 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 - else _Rft_crc_sent = false end + return true, (2 << 14), self.aid, c, (high_byte << 8) | low_byte + 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 word2 = ((seg & 0xFF) << 8) | b(0) + local word1 = (((self.toggle and 1 or 0) << 7) | ((self.file_segment >> 8) & 0x7F)) + local word2 = ((self.file_segment & 0xFF) << 8) | b(0) local word3 = (b(1) << 8) | b(2) local word4 = (b(3) << 8) | b(4) - _Rft_file_segment = seg + 1 - if _Rft_file_segment >= total_segments then - _Rft_file_segment = 0 - if _Rft_send_once then stop_rft() end + self.file_segment = self.file_segment + 1 + if self.file_segment >= total_segments then + self.file_segment = 0 + if self.send_once then self:stop() end end return true, (2 << 12) | word1, word2, word3, word4 @@ -84,99 +107,62 @@ local function start_rft() end 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) - local interrupted = (#_Rft_file ~= 0) +--- Loads a file and begins RDS2 transmission. +---@param aid integer Application ID (e.g. 0xFF7F for Station Logo) +---@param path string System path to the file +---@param id integer File ID (0-63). Use the same ID to update a file, different to trigger a reset. +---@param crc integer|boolean CRC Mode (0: Full, 1-5: Chunks, true/7: Auto) +---@param once boolean If true, file is sent once and the stream is closed +---@return boolean interrupted Returns true if a previous file was already being sent +function RftInstance:sendFile(aid, path, id, crc, once) + local interrupted = (#self.file_data ~= 0) - if _Rft_aid ~= aid then stop_rft() end - _Rft_aid = aid + if self.aid ~= aid then self:stop() end + self.aid = aid local file = io.open(path, "rb") - if not file then error("Could not open file") end - _Rft_file = file:read("*a") + if not file then error("Could not open file: " .. path) end + self.file_data = file:read("*a") file:close() - _Rft_send_once = once + self.send_once = once - if id == _Rft_last_id then - _Rft_toggle = not _Rft_toggle - _Rft_crc_sent = 0 - _Rft_version = _Rft_version + 1 - if _Rft_version > 7 then _Rft_version = 0 end + if id == self.last_id then + self.toggle = not self.toggle + self.version = (self.version + 1) % 8 end - _Rft_crc_data = "" - _Rft_crc = (crc ~= false) + self.crc_data = "" + self.crc_enabled = (crc ~= false) - local chunk_size = 0 - if crc and crc == 0 then - _Rft_crc_mode = 0 - _Rft_crc_full_file = crc16(_Rft_file) - elseif crc and crc == 1 and #_Rft_file <= 40960 then - _Rft_crc_mode = 1 - chunk_size = 5 * 16 - elseif crc and crc == 2 and #_Rft_file < 40960 and #_Rft_file >= 81920 then - _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 + local f_size = #self.file_data + if crc == 0 then + self.crc_mode = 0 + self.crc_full_file = crc16(self.file_data) + elseif crc == true or crc == 7 then + if f_size <= 40960 then self.crc_mode = 1 + elseif f_size > 40960 and f_size <= 81920 then self.crc_mode = 2 + else self.crc_mode = 3 end else - _Rft_crc = false + self.crc_mode = crc and 1 or 0 end - if _Rft_crc and chunk_size ~= 0 then - for i = 1, #_Rft_file, chunk_size do - local chunk = string.sub(_Rft_file, i, i + chunk_size - 1) - local crc_val = crc16(chunk) - _Rft_crc_data = _Rft_crc_data .. string.char(math.floor(crc_val / 256), crc_val % 256) + local chunk_multipliers = {16, 32, 64, 128, 256} + local multiplier = chunk_multipliers[self.crc_mode] + if self.crc_enabled and multiplier then + local chunk_size = 5 * multiplier + 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 - if #_Rft_file > 262143 then error("The file is too large", 2) end - if _Rft_oda_id == nil then start_rft() 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 + if f_size > 262143 then error("File too large") end + if self.oda_id == nil then self:start() end - _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 -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 +end \ No newline at end of file