-- Copyright (c) 2026 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.
local validate = require 'mc.validate'
local utils = require 'mc.utils'
local mdb = require 'mc.mdb'
local create_enum_type = require 'mc.enum'

local EyeDiagram = {}

---@class EyeDiagram.ScopeSel: Enum
local EScopeSel = create_enum_type('ScopeSel')
EScopeSel.default = EScopeSel.new(2147483647)
EScopeSel.struct = nil
EScopeSel.BeforeAFESampling = EScopeSel.new(0)
EScopeSel.AfterAFESampling = EScopeSel.new(1)

EyeDiagram.ScopeSel = EScopeSel

---@class EyeDiagram.PortType: Enum
local EPortType = create_enum_type('PortType')
EPortType.default = EPortType.new(2147483647)
EPortType.struct = nil
EPortType.PCIe0 = EPortType.new(0)
EPortType.PCIe1 = EPortType.new(1)
EPortType.GMAC2 = EPortType.new(2)
EPortType.GMAC3 = EPortType.new(3)
EPortType.USB = EPortType.new(4)

EyeDiagram.PortType = EPortType

---@class EyeDiagram.FourEyeDiagramStruct
---@field Bottom integer
---@field Top integer
---@field Left integer
---@field Right integer
local TFourEyeDiagramStruct = {}
TFourEyeDiagramStruct.__index = TFourEyeDiagramStruct
TFourEyeDiagramStruct.group = {}

local function TFourEyeDiagramStruct_from_obj(obj)
    return setmetatable(obj, TFourEyeDiagramStruct)
end

function TFourEyeDiagramStruct.new(Bottom, Top, Left, Right)
    return TFourEyeDiagramStruct_from_obj({Bottom = Bottom, Top = Top, Left = Left, Right = Right})
end
---@param obj EyeDiagram.FourEyeDiagramStruct
function TFourEyeDiagramStruct:init_from_obj(obj)
    self.Bottom = obj.Bottom
    self.Top = obj.Top
    self.Left = obj.Left
    self.Right = obj.Right
end

function TFourEyeDiagramStruct:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TFourEyeDiagramStruct.group)
end

TFourEyeDiagramStruct.from_obj = TFourEyeDiagramStruct_from_obj

TFourEyeDiagramStruct.proto_property = {'Bottom', 'Top', 'Left', 'Right'}

TFourEyeDiagramStruct.default = {0, 0, 0, 0}

TFourEyeDiagramStruct.struct = {
    {name = 'Bottom', is_array = false, struct = nil}, {name = 'Top', is_array = false, struct = nil},
    {name = 'Left', is_array = false, struct = nil}, {name = 'Right', is_array = false, struct = nil}
}

function TFourEyeDiagramStruct:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'Bottom', self.Bottom, 'int32', false, errs, need_convert)
    validate.Optional(prefix .. 'Top', self.Top, 'int32', false, errs, need_convert)
    validate.Optional(prefix .. 'Left', self.Left, 'int32', false, errs, need_convert)
    validate.Optional(prefix .. 'Right', self.Right, 'int32', false, errs, need_convert)

    TFourEyeDiagramStruct:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TFourEyeDiagramStruct.proto_property, errs, need_convert)
    return self
end

function TFourEyeDiagramStruct:unpack(_)
    return self.Bottom, self.Top, self.Left, self.Right
end

EyeDiagram.FourEyeDiagramStruct = TFourEyeDiagramStruct

---@class EyeDiagram.SerdesEyeArgsStruct
---@field ChipID integer
---@field MacroID integer
---@field LaneID integer
---@field ScanMode integer
---@field PreTap integer
---@field PatternType integer
---@field EyeDiagramType integer
---@field PathType integer
---@field WindowSize integer
---@field Threshold integer
---@field AvgSamplingCount integer
local TSerdesEyeArgsStruct = {}
TSerdesEyeArgsStruct.__index = TSerdesEyeArgsStruct
TSerdesEyeArgsStruct.group = {}

local function TSerdesEyeArgsStruct_from_obj(obj)
    return setmetatable(obj, TSerdesEyeArgsStruct)
end

function TSerdesEyeArgsStruct.new(ChipID, MacroID, LaneID, ScanMode, PreTap, PatternType, EyeDiagramType, PathType,
    WindowSize, Threshold, AvgSamplingCount)
    return TSerdesEyeArgsStruct_from_obj({
        ChipID = ChipID,
        MacroID = MacroID,
        LaneID = LaneID,
        ScanMode = ScanMode,
        PreTap = PreTap,
        PatternType = PatternType,
        EyeDiagramType = EyeDiagramType,
        PathType = PathType,
        WindowSize = WindowSize,
        Threshold = Threshold,
        AvgSamplingCount = AvgSamplingCount
    })
end
---@param obj EyeDiagram.SerdesEyeArgsStruct
function TSerdesEyeArgsStruct:init_from_obj(obj)
    self.ChipID = obj.ChipID
    self.MacroID = obj.MacroID
    self.LaneID = obj.LaneID
    self.ScanMode = obj.ScanMode
    self.PreTap = obj.PreTap
    self.PatternType = obj.PatternType
    self.EyeDiagramType = obj.EyeDiagramType
    self.PathType = obj.PathType
    self.WindowSize = obj.WindowSize
    self.Threshold = obj.Threshold
    self.AvgSamplingCount = obj.AvgSamplingCount
end

function TSerdesEyeArgsStruct:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TSerdesEyeArgsStruct.group)
end

TSerdesEyeArgsStruct.from_obj = TSerdesEyeArgsStruct_from_obj

TSerdesEyeArgsStruct.proto_property = {
    'ChipID', 'MacroID', 'LaneID', 'ScanMode', 'PreTap', 'PatternType', 'EyeDiagramType', 'PathType', 'WindowSize',
    'Threshold', 'AvgSamplingCount'
}

TSerdesEyeArgsStruct.default = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

TSerdesEyeArgsStruct.struct = {
    {name = 'ChipID', is_array = false, struct = nil}, {name = 'MacroID', is_array = false, struct = nil},
    {name = 'LaneID', is_array = false, struct = nil}, {name = 'ScanMode', is_array = false, struct = nil},
    {name = 'PreTap', is_array = false, struct = nil}, {name = 'PatternType', is_array = false, struct = nil},
    {name = 'EyeDiagramType', is_array = false, struct = nil}, {name = 'PathType', is_array = false, struct = nil},
    {name = 'WindowSize', is_array = false, struct = nil}, {name = 'Threshold', is_array = false, struct = nil},
    {name = 'AvgSamplingCount', is_array = false, struct = nil}
}

function TSerdesEyeArgsStruct:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'ChipID', self.ChipID, 'uint16', false, errs, need_convert)
    validate.Optional(prefix .. 'MacroID', self.MacroID, 'uint16', false, errs, need_convert)
    validate.Optional(prefix .. 'LaneID', self.LaneID, 'uint16', false, errs, need_convert)
    validate.Optional(prefix .. 'ScanMode', self.ScanMode, 'uint8', false, errs, need_convert)
    validate.Optional(prefix .. 'PreTap', self.PreTap, 'uint8', false, errs, need_convert)
    validate.Optional(prefix .. 'PatternType', self.PatternType, 'uint8', false, errs, need_convert)
    validate.Optional(prefix .. 'EyeDiagramType', self.EyeDiagramType, 'uint8', false, errs, need_convert)
    validate.Optional(prefix .. 'PathType', self.PathType, 'uint8', false, errs, need_convert)
    validate.Optional(prefix .. 'WindowSize', self.WindowSize, 'uint32', false, errs, need_convert)
    validate.Optional(prefix .. 'Threshold', self.Threshold, 'uint32', false, errs, need_convert)
    validate.Optional(prefix .. 'AvgSamplingCount', self.AvgSamplingCount, 'uint32', false, errs, need_convert)

    TSerdesEyeArgsStruct:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TSerdesEyeArgsStruct.proto_property, errs, need_convert)
    return self
end

function TSerdesEyeArgsStruct:unpack(_)
    return self.ChipID, self.MacroID, self.LaneID, self.ScanMode, self.PreTap, self.PatternType, self.EyeDiagramType,
        self.PathType, self.WindowSize, self.Threshold, self.AvgSamplingCount
end

EyeDiagram.SerdesEyeArgsStruct = TSerdesEyeArgsStruct

---@class EyeDiagram.GetFourEyeDiagramRsp
---@field FourEyeDiagram EyeDiagram.FourEyeDiagramStruct
local TGetFourEyeDiagramRsp = {}
TGetFourEyeDiagramRsp.__index = TGetFourEyeDiagramRsp
TGetFourEyeDiagramRsp.group = {}

local function TGetFourEyeDiagramRsp_from_obj(obj)
    obj.FourEyeDiagram = utils.from_obj(EyeDiagram.FourEyeDiagramStruct, obj.FourEyeDiagram)
    return setmetatable(obj, TGetFourEyeDiagramRsp)
end

function TGetFourEyeDiagramRsp.new(FourEyeDiagram)
    return TGetFourEyeDiagramRsp_from_obj({FourEyeDiagram = FourEyeDiagram})
end
---@param obj EyeDiagram.GetFourEyeDiagramRsp
function TGetFourEyeDiagramRsp:init_from_obj(obj)
    self.FourEyeDiagram = obj.FourEyeDiagram
end

function TGetFourEyeDiagramRsp:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TGetFourEyeDiagramRsp.group)
end

TGetFourEyeDiagramRsp.from_obj = TGetFourEyeDiagramRsp_from_obj

TGetFourEyeDiagramRsp.proto_property = {'FourEyeDiagram'}

TGetFourEyeDiagramRsp.default = {EyeDiagram.FourEyeDiagramStruct.default}

TGetFourEyeDiagramRsp.struct = {
    {name = 'FourEyeDiagram', is_array = false, struct = EyeDiagram.FourEyeDiagramStruct.struct}
}

function TGetFourEyeDiagramRsp:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    EyeDiagram.FourEyeDiagramStruct.new(self.FourEyeDiagram.Bottom, self.FourEyeDiagram.Top, self.FourEyeDiagram.Left,
        self.FourEyeDiagram.Right):validate(prefix, errs, need_convert)

    TGetFourEyeDiagramRsp:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TGetFourEyeDiagramRsp.proto_property, errs, need_convert)
    return self
end

function TGetFourEyeDiagramRsp:unpack(raw)
    return utils.unpack(raw, self.FourEyeDiagram)
end

EyeDiagram.GetFourEyeDiagramRsp = TGetFourEyeDiagramRsp

---@class EyeDiagram.GetFourEyeDiagramReq
---@field SerdesEyeArgs EyeDiagram.SerdesEyeArgsStruct
local TGetFourEyeDiagramReq = {}
TGetFourEyeDiagramReq.__index = TGetFourEyeDiagramReq
TGetFourEyeDiagramReq.group = {}

local function TGetFourEyeDiagramReq_from_obj(obj)
    obj.SerdesEyeArgs = utils.from_obj(EyeDiagram.SerdesEyeArgsStruct, obj.SerdesEyeArgs)
    return setmetatable(obj, TGetFourEyeDiagramReq)
end

function TGetFourEyeDiagramReq.new(SerdesEyeArgs)
    return TGetFourEyeDiagramReq_from_obj({SerdesEyeArgs = SerdesEyeArgs})
end
---@param obj EyeDiagram.GetFourEyeDiagramReq
function TGetFourEyeDiagramReq:init_from_obj(obj)
    self.SerdesEyeArgs = obj.SerdesEyeArgs
end

function TGetFourEyeDiagramReq:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TGetFourEyeDiagramReq.group)
end

TGetFourEyeDiagramReq.from_obj = TGetFourEyeDiagramReq_from_obj

TGetFourEyeDiagramReq.proto_property = {'SerdesEyeArgs'}

TGetFourEyeDiagramReq.default = {EyeDiagram.SerdesEyeArgsStruct.default}

TGetFourEyeDiagramReq.struct = {
    {name = 'SerdesEyeArgs', is_array = false, struct = EyeDiagram.SerdesEyeArgsStruct.struct}
}

function TGetFourEyeDiagramReq:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    EyeDiagram.SerdesEyeArgsStruct.new(self.SerdesEyeArgs.ChipID, self.SerdesEyeArgs.MacroID, self.SerdesEyeArgs.LaneID,
        self.SerdesEyeArgs.ScanMode, self.SerdesEyeArgs.PreTap, self.SerdesEyeArgs.PatternType,
        self.SerdesEyeArgs.EyeDiagramType, self.SerdesEyeArgs.PathType, self.SerdesEyeArgs.WindowSize,
        self.SerdesEyeArgs.Threshold, self.SerdesEyeArgs.AvgSamplingCount):validate(prefix, errs, need_convert)

    TGetFourEyeDiagramReq:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TGetFourEyeDiagramReq.proto_property, errs, need_convert)
    return self
end

function TGetFourEyeDiagramReq:unpack(raw)
    return utils.unpack(raw, self.SerdesEyeArgs)
end

EyeDiagram.GetFourEyeDiagramReq = TGetFourEyeDiagramReq

---@class EyeDiagram.GetResultRsp
---@field Result string
local TGetResultRsp = {}
TGetResultRsp.__index = TGetResultRsp
TGetResultRsp.group = {}

local function TGetResultRsp_from_obj(obj)
    return setmetatable(obj, TGetResultRsp)
end

function TGetResultRsp.new(Result)
    return TGetResultRsp_from_obj({Result = Result})
end
---@param obj EyeDiagram.GetResultRsp
function TGetResultRsp:init_from_obj(obj)
    self.Result = obj.Result
end

function TGetResultRsp:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TGetResultRsp.group)
end

TGetResultRsp.from_obj = TGetResultRsp_from_obj

TGetResultRsp.proto_property = {'Result'}

TGetResultRsp.default = {''}

TGetResultRsp.struct = {{name = 'Result', is_array = false, struct = nil}}

function TGetResultRsp:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'Result', self.Result, 'string', false, errs, need_convert)

    TGetResultRsp:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TGetResultRsp.proto_property, errs, need_convert)
    return self
end

function TGetResultRsp:unpack(_)
    return self.Result
end

EyeDiagram.GetResultRsp = TGetResultRsp

---@class EyeDiagram.GetResultReq
---@field Row integer
---@field Cols integer
---@field CompareValue integer
local TGetResultReq = {}
TGetResultReq.__index = TGetResultReq
TGetResultReq.group = {}

local function TGetResultReq_from_obj(obj)
    return setmetatable(obj, TGetResultReq)
end

function TGetResultReq.new(Row, Cols, CompareValue)
    return TGetResultReq_from_obj({Row = Row, Cols = Cols, CompareValue = CompareValue})
end
---@param obj EyeDiagram.GetResultReq
function TGetResultReq:init_from_obj(obj)
    self.Row = obj.Row
    self.Cols = obj.Cols
    self.CompareValue = obj.CompareValue
end

function TGetResultReq:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TGetResultReq.group)
end

TGetResultReq.from_obj = TGetResultReq_from_obj

TGetResultReq.proto_property = {'Row', 'Cols', 'CompareValue'}

TGetResultReq.default = {0, 0, 0}

TGetResultReq.struct = {
    {name = 'Row', is_array = false, struct = nil}, {name = 'Cols', is_array = false, struct = nil},
    {name = 'CompareValue', is_array = false, struct = nil}
}

function TGetResultReq:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'Row', self.Row, 'int32', false, errs, need_convert)
    validate.Optional(prefix .. 'Cols', self.Cols, 'int32', false, errs, need_convert)
    validate.Optional(prefix .. 'CompareValue', self.CompareValue, 'int32', false, errs, need_convert)

    TGetResultReq:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TGetResultReq.proto_property, errs, need_convert)
    return self
end

function TGetResultReq:unpack(_)
    return self.Row, self.Cols, self.CompareValue
end

EyeDiagram.GetResultReq = TGetResultReq

---@class EyeDiagram.StartTestRsp
---@field Row integer
---@field Cols integer
local TStartTestRsp = {}
TStartTestRsp.__index = TStartTestRsp
TStartTestRsp.group = {}

local function TStartTestRsp_from_obj(obj)
    return setmetatable(obj, TStartTestRsp)
end

function TStartTestRsp.new(Row, Cols)
    return TStartTestRsp_from_obj({Row = Row, Cols = Cols})
end
---@param obj EyeDiagram.StartTestRsp
function TStartTestRsp:init_from_obj(obj)
    self.Row = obj.Row
    self.Cols = obj.Cols
end

function TStartTestRsp:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TStartTestRsp.group)
end

TStartTestRsp.from_obj = TStartTestRsp_from_obj

TStartTestRsp.proto_property = {'Row', 'Cols'}

TStartTestRsp.default = {0, 0}

TStartTestRsp.struct = {{name = 'Row', is_array = false, struct = nil}, {name = 'Cols', is_array = false, struct = nil}}

function TStartTestRsp:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'Row', self.Row, 'int32', false, errs, need_convert)
    validate.Optional(prefix .. 'Cols', self.Cols, 'int32', false, errs, need_convert)

    TStartTestRsp:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TStartTestRsp.proto_property, errs, need_convert)
    return self
end

function TStartTestRsp:unpack(_)
    return self.Row, self.Cols
end

EyeDiagram.StartTestRsp = TStartTestRsp

---@class EyeDiagram.StartTestReq
---@field Type EyeDiagram.PortType
---@field ScopeSel EyeDiagram.ScopeSel
local TStartTestReq = {}
TStartTestReq.__index = TStartTestReq
TStartTestReq.group = {}

local function TStartTestReq_from_obj(obj)
    obj.Type = obj.Type and EyeDiagram.PortType.new(obj.Type)
    obj.ScopeSel = obj.ScopeSel and EyeDiagram.ScopeSel.new(obj.ScopeSel)
    return setmetatable(obj, TStartTestReq)
end

function TStartTestReq.new(Type, ScopeSel)
    return TStartTestReq_from_obj({Type = Type, ScopeSel = ScopeSel})
end
---@param obj EyeDiagram.StartTestReq
function TStartTestReq:init_from_obj(obj)
    self.Type = obj.Type
    self.ScopeSel = obj.ScopeSel
end

function TStartTestReq:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TStartTestReq.group)
end

TStartTestReq.from_obj = TStartTestReq_from_obj

TStartTestReq.proto_property = {'Type', 'ScopeSel'}

TStartTestReq.default = {EyeDiagram.PortType.default, EyeDiagram.ScopeSel.default}

TStartTestReq.struct = {
    {name = 'Type', is_array = false, struct = EyeDiagram.PortType.struct},
    {name = 'ScopeSel', is_array = false, struct = EyeDiagram.ScopeSel.struct}
}

function TStartTestReq:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'Type', self.Type, 'EyeDiagram.PortType', false, errs, need_convert)
    validate.Optional(prefix .. 'ScopeSel', self.ScopeSel, 'EyeDiagram.ScopeSel', false, errs, need_convert)

    TStartTestReq:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TStartTestReq.proto_property, errs, need_convert)
    return self
end

function TStartTestReq:unpack(raw)
    local Type = utils.unpack_enum(raw, self.Type)
    local ScopeSel = utils.unpack_enum(raw, self.ScopeSel)
    return Type, ScopeSel
end

EyeDiagram.StartTestReq = TStartTestReq

EyeDiagram.interface = mdb.register_interface('bmc.kepler.Managers.SOC.Serdes.EyeDiagram', {}, {
    StartTest = {'a{ss}ii', 'ii', TStartTestReq, TStartTestRsp},
    GetResult = {'a{ss}iii', 's', TGetResultReq, TGetResultRsp},
    GetFourEyeDiagram = {'a{ss}(qqqyyyyyuuu)', '(iiii)', TGetFourEyeDiagramReq, TGetFourEyeDiagramRsp}
}, {})

return EyeDiagram
