-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

-- Description: 定义模块需要的通用常量和方法
local dirty_test_def = {}
dirty_test_def.cmd = {
    GET_CAPABILITY = 0,
    GET_RESULT = 1,
    INIT = 2,
    START = 3,
    CLOSE = 4,
    PHASE1_GET_FFT = 11,
    PHASE1_SET_FFT = 12,
    PHASE2_GET_FFT = 13,
    PHASE2_SET_FFT = 14
}

dirty_test_def.rsp_code = {
    SUCCESS = 0,
    FAIL = 1,
    UNSUPPORT = 2
}

dirty_test_def.dirty_support_type = {
    NOT_SUPPORT = 0,    -- 不支持
    SUPPORT = 1     -- 支持
}

-- start命令转化为指令
dirty_test_def.start_action_to_item_map = {
    ['DetectionPhase1']     = 0,
    ['InitiationPhase2']    = 1,
    ['DetectionPhase2']     = 2
}

-- 状态机
dirty_test_def.state_machine = {
    ['Abnormal']            = -1, -- 执行异常(含软、硬件异常，不区分阶段)
    ['Pending']             = 0, -- 未初始化或已退出检测（所有配置回退）
    ['Phase1Initialized']   = 1, -- 阶段一已初始化
    ['Phase1Running']       = 5, -- 阶段一运行中
    ['Phase1Completed']     = 2, -- 阶段一完成(fft写入)
    ['Phase2Initialized']   = 3, -- 阶段二已初始化
    ['Phase2Running']       = 6, -- 阶段二运行中
    ['Phase2Completed']     = 4, -- 阶段二完成(fft写入)
}

-- start命令预期抵达的目标状态机
dirty_test_def.start_param_to_machine_map = {
    ['InitiationPhase1']    = dirty_test_def.state_machine.Phase1Initialized,
    ['DetectionPhase1']     = dirty_test_def.state_machine.Phase1Running,
    ['InitiationPhase2']    = dirty_test_def.state_machine.Phase2Initialized,
    ['DetectionPhase2']     = dirty_test_def.state_machine.Phase2Running
}

dirty_test_def.stage = {
    [dirty_test_def.state_machine.Abnormal]             = 'None',
    [dirty_test_def.state_machine.Pending]              = 'None',
    [dirty_test_def.state_machine.Phase1Initialized]    = 'Phase1',
    [dirty_test_def.state_machine.Phase1Running]        = 'Phase1',
    [dirty_test_def.state_machine.Phase1Completed]      = 'Phase1',
    [dirty_test_def.state_machine.Phase2Initialized]    = 'Phase2',
    [dirty_test_def.state_machine.Phase2Running]        = 'Phase2',
    [dirty_test_def.state_machine.Phase2Completed]      = 'Phase2'
}

dirty_test_def.stage_status = {
    [dirty_test_def.state_machine.Abnormal]             = 'Unknown',
    [dirty_test_def.state_machine.Pending]              = 'Pending',
    [dirty_test_def.state_machine.Phase1Initialized]    = 'Pending',
    [dirty_test_def.state_machine.Phase1Running]        = 'Running',
    [dirty_test_def.state_machine.Phase1Completed]      = 'Completed',
    [dirty_test_def.state_machine.Phase2Initialized]    = 'Pending',
    [dirty_test_def.state_machine.Phase2Running]        = 'Running',
    [dirty_test_def.state_machine.Phase2Completed]      = 'Completed'
}

dirty_test_def.dirty_result = {
    ['Unknown'] = 'Unknown',
    ['Detected'] = 'Detected',
    ['NotDetected'] = 'NotDetected'
}

--[[
LPO:
 <------------------------------------------
 ¦     ¦     ¦     ¦     ¦     ¦     ¦     ¦
-1 --> 0 --> 1 --> 5 --> 2 --> 3 --> 6 --> 4
       ¦     ¦     ¦     ¦     ¦     ¦     ¦
       <------------------------------------
oDSP:
增加 5 --> 4
]]
dirty_test_def.state_machine_trans_map = {
    ['LPO'] = {
        [-1] = {[0] = true},
        [0] = {[1] = true, [-1] = true},
        [1] = {[5] = true, [-1] = true, [0] = true},
        [5] = {[2] = true, [-1] = true, [0] = true},
        [2] = {[3] = true, [-1] = true, [0] = true},
        [3] = {[6] = true, [-1] = true, [0] = true},
        [6] = {[4] = true, [-1] = true, [0] = true},
        [4] = {[-1] = true, [0] = true},
    },
    ['oDSP'] = {
        [-1] = {[0] = true},
        [0] = {[1] = true, [-1] = true},
        [1] = {[5] = true, [-1] = true, [0] = true},
        [5] = {[4] = true, [2] = true, [-1] = true, [0] = true},
        [2] = {[3] = true, [-1] = true, [0] = true},
        [3] = {[6] = true, [-1] = true, [0] = true},
        [6] = {[4] = true, [-1] = true, [0] = true},
        [4] = {[-1] = true, [0] = true},
    }
}

return dirty_test_def