-- Script V1 : 26_03_30

print('START...')

local rc, currentBR = can.getCurrentBaudrate()
if (currentBR == 0) then
    --Ask the user here, since there is no current CAN baudrate set and validated
    currentBR = ui.inputValue('Set CAN Baudrate', 'u', 1, 500000, 500000)
end

if (currentBR == nil) then 
    error('No CAN Baudrate provided.')
end

rc = can.init(currentBR)
if (rc ~= 0) then
    error("CAN Init failed! Error: " .. rc .. " (Invalid BUS / Invalid CAN baudrate / Key off)")
end

-- Set CAN filters to bypass mode
rc = can.disableFilters()
if (rc ~= 0) then
    error( string.format("Failed to disable filters! Error: %d", rc))
end


-- START of the logging
ui.sleep(100 * 1)

rc = can.startLog('CANLog1m.log')
if (rc ~= 0) then
    error( string.format("Failed to start CAN log! Error: %d", rc))
end

ui.sleep(100 * 1)

-- capture 1 minutes (1 * 60 seconds) of data with no filter active to have a base status for the next analysis
print('Starting 1 minute capture...')
ui.sleep(100 * 1 * 60)

--STOP
rc = can.stopLog()
if (rc ~= 0) then
    error( string.format("Failed to stop CAN log! Error: %d", rc))
end


rc = can.enableFilters()
can.uninit() -- since we do not care about uninit rc, we can put it here and later check the errors of the previous function
if (rc ~= 0) then
    error( string.format("Failed to enable filters! Error: %d", rc))
end

print('DONE.')