-- 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 FlashIO = {}

---@class FlashIO.RawWriteRsp
local TRawWriteRsp = {}
TRawWriteRsp.__index = TRawWriteRsp
TRawWriteRsp.group = {}

local function TRawWriteRsp_from_obj(obj)
    return setmetatable(obj, TRawWriteRsp)
end

function TRawWriteRsp.new()
    return TRawWriteRsp_from_obj({})
end
---@param obj FlashIO.RawWriteRsp
function TRawWriteRsp:init_from_obj(obj)

end

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

TRawWriteRsp.from_obj = TRawWriteRsp_from_obj

TRawWriteRsp.proto_property = {}

TRawWriteRsp.default = {}

TRawWriteRsp.struct = {}

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

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

function TRawWriteRsp:unpack(_)
end

FlashIO.RawWriteRsp = TRawWriteRsp

---@class FlashIO.RawWriteReq
---@field InData integer[]
local TRawWriteReq = {}
TRawWriteReq.__index = TRawWriteReq
TRawWriteReq.group = {}

local function TRawWriteReq_from_obj(obj)
    return setmetatable(obj, TRawWriteReq)
end

function TRawWriteReq.new(InData)
    return TRawWriteReq_from_obj({InData = InData})
end
---@param obj FlashIO.RawWriteReq
function TRawWriteReq:init_from_obj(obj)
    self.InData = obj.InData
end

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

TRawWriteReq.from_obj = TRawWriteReq_from_obj

TRawWriteReq.proto_property = {'InData'}

TRawWriteReq.default = {{}}

TRawWriteReq.struct = {{name = 'InData', is_array = true, struct = nil}}

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

    validate.OptionalArray(prefix .. 'InData', self.InData, 'uint8', false, errs, need_convert)

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

function TRawWriteReq:unpack(_)
    return self.InData
end

FlashIO.RawWriteReq = TRawWriteReq

---@class FlashIO.RawReadRsp
---@field OutData integer[]
local TRawReadRsp = {}
TRawReadRsp.__index = TRawReadRsp
TRawReadRsp.group = {}

local function TRawReadRsp_from_obj(obj)
    return setmetatable(obj, TRawReadRsp)
end

function TRawReadRsp.new(OutData)
    return TRawReadRsp_from_obj({OutData = OutData})
end
---@param obj FlashIO.RawReadRsp
function TRawReadRsp:init_from_obj(obj)
    self.OutData = obj.OutData
end

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

TRawReadRsp.from_obj = TRawReadRsp_from_obj

TRawReadRsp.proto_property = {'OutData'}

TRawReadRsp.default = {{}}

TRawReadRsp.struct = {{name = 'OutData', is_array = true, struct = nil}}

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

    validate.OptionalArray(prefix .. 'OutData', self.OutData, 'uint8', false, errs, need_convert)

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

function TRawReadRsp:unpack(_)
    return self.OutData
end

FlashIO.RawReadRsp = TRawReadRsp

---@class FlashIO.RawReadReq
---@field Length integer
---@field InData integer[]
local TRawReadReq = {}
TRawReadReq.__index = TRawReadReq
TRawReadReq.group = {}

local function TRawReadReq_from_obj(obj)
    return setmetatable(obj, TRawReadReq)
end

function TRawReadReq.new(Length, InData)
    return TRawReadReq_from_obj({Length = Length, InData = InData})
end
---@param obj FlashIO.RawReadReq
function TRawReadReq:init_from_obj(obj)
    self.Length = obj.Length
    self.InData = obj.InData
end

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

TRawReadReq.from_obj = TRawReadReq_from_obj

TRawReadReq.proto_property = {'Length', 'InData'}

TRawReadReq.default = {0, {}}

TRawReadReq.struct = {
    {name = 'Length', is_array = false, struct = nil}, {name = 'InData', is_array = true, struct = nil}
}

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

    validate.Optional(prefix .. 'Length', self.Length, 'uint32', false, errs, need_convert)
    validate.OptionalArray(prefix .. 'InData', self.InData, 'uint8', false, errs, need_convert)

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

function TRawReadReq:unpack(_)
    return self.Length, self.InData
end

FlashIO.RawReadReq = TRawReadReq

---@class FlashIO.EraseRsp
local TEraseRsp = {}
TEraseRsp.__index = TEraseRsp
TEraseRsp.group = {}

local function TEraseRsp_from_obj(obj)
    return setmetatable(obj, TEraseRsp)
end

function TEraseRsp.new()
    return TEraseRsp_from_obj({})
end
---@param obj FlashIO.EraseRsp
function TEraseRsp:init_from_obj(obj)

end

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

TEraseRsp.from_obj = TEraseRsp_from_obj

TEraseRsp.proto_property = {}

TEraseRsp.default = {}

TEraseRsp.struct = {}

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

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

function TEraseRsp:unpack(_)
end

FlashIO.EraseRsp = TEraseRsp

---@class FlashIO.EraseReq
---@field Offset integer
---@field Type integer
local TEraseReq = {}
TEraseReq.__index = TEraseReq
TEraseReq.group = {}

local function TEraseReq_from_obj(obj)
    return setmetatable(obj, TEraseReq)
end

function TEraseReq.new(Offset, Type)
    return TEraseReq_from_obj({Offset = Offset, Type = Type})
end
---@param obj FlashIO.EraseReq
function TEraseReq:init_from_obj(obj)
    self.Offset = obj.Offset
    self.Type = obj.Type
end

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

TEraseReq.from_obj = TEraseReq_from_obj

TEraseReq.proto_property = {'Offset', 'Type'}

TEraseReq.default = {0, 0}

TEraseReq.struct = {{name = 'Offset', is_array = false, struct = nil}, {name = 'Type', is_array = false, struct = nil}}

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

    validate.Optional(prefix .. 'Offset', self.Offset, 'uint32', false, errs, need_convert)
    validate.Optional(prefix .. 'Type', self.Type, 'uint8', false, errs, need_convert)

    if self.Type ~= nil then
        validate.Enum(prefix .. 'Type', self.Type, '', {0, 1, 2, 3}, errs, need_convert)
    end

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

function TEraseReq:unpack(_)
    return self.Offset, self.Type
end

FlashIO.EraseReq = TEraseReq

---@class FlashIO.WriteRsp
local TWriteRsp = {}
TWriteRsp.__index = TWriteRsp
TWriteRsp.group = {}

local function TWriteRsp_from_obj(obj)
    return setmetatable(obj, TWriteRsp)
end

function TWriteRsp.new()
    return TWriteRsp_from_obj({})
end
---@param obj FlashIO.WriteRsp
function TWriteRsp:init_from_obj(obj)

end

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

TWriteRsp.from_obj = TWriteRsp_from_obj

TWriteRsp.proto_property = {}

TWriteRsp.default = {}

TWriteRsp.struct = {}

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

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

function TWriteRsp:unpack(_)
end

FlashIO.WriteRsp = TWriteRsp

---@class FlashIO.WriteReq
---@field Offset integer
---@field InData integer[]
local TWriteReq = {}
TWriteReq.__index = TWriteReq
TWriteReq.group = {}

local function TWriteReq_from_obj(obj)
    return setmetatable(obj, TWriteReq)
end

function TWriteReq.new(Offset, InData)
    return TWriteReq_from_obj({Offset = Offset, InData = InData})
end
---@param obj FlashIO.WriteReq
function TWriteReq:init_from_obj(obj)
    self.Offset = obj.Offset
    self.InData = obj.InData
end

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

TWriteReq.from_obj = TWriteReq_from_obj

TWriteReq.proto_property = {'Offset', 'InData'}

TWriteReq.default = {0, {}}

TWriteReq.struct = {{name = 'Offset', is_array = false, struct = nil}, {name = 'InData', is_array = true, struct = nil}}

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

    validate.Optional(prefix .. 'Offset', self.Offset, 'uint32', false, errs, need_convert)
    validate.OptionalArray(prefix .. 'InData', self.InData, 'uint8', false, errs, need_convert)

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

function TWriteReq:unpack(_)
    return self.Offset, self.InData
end

FlashIO.WriteReq = TWriteReq

---@class FlashIO.ReadRsp
---@field OutData integer[]
local TReadRsp = {}
TReadRsp.__index = TReadRsp
TReadRsp.group = {}

local function TReadRsp_from_obj(obj)
    return setmetatable(obj, TReadRsp)
end

function TReadRsp.new(OutData)
    return TReadRsp_from_obj({OutData = OutData})
end
---@param obj FlashIO.ReadRsp
function TReadRsp:init_from_obj(obj)
    self.OutData = obj.OutData
end

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

TReadRsp.from_obj = TReadRsp_from_obj

TReadRsp.proto_property = {'OutData'}

TReadRsp.default = {{}}

TReadRsp.struct = {{name = 'OutData', is_array = true, struct = nil}}

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

    validate.OptionalArray(prefix .. 'OutData', self.OutData, 'uint8', false, errs, need_convert)

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

function TReadRsp:unpack(_)
    return self.OutData
end

FlashIO.ReadRsp = TReadRsp

---@class FlashIO.ReadReq
---@field Offset integer
---@field Length integer
local TReadReq = {}
TReadReq.__index = TReadReq
TReadReq.group = {}

local function TReadReq_from_obj(obj)
    return setmetatable(obj, TReadReq)
end

function TReadReq.new(Offset, Length)
    return TReadReq_from_obj({Offset = Offset, Length = Length})
end
---@param obj FlashIO.ReadReq
function TReadReq:init_from_obj(obj)
    self.Offset = obj.Offset
    self.Length = obj.Length
end

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

TReadReq.from_obj = TReadReq_from_obj

TReadReq.proto_property = {'Offset', 'Length'}

TReadReq.default = {0, 0}

TReadReq.struct = {{name = 'Offset', is_array = false, struct = nil}, {name = 'Length', is_array = false, struct = nil}}

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

    validate.Optional(prefix .. 'Offset', self.Offset, 'uint32', false, errs, need_convert)
    validate.Optional(prefix .. 'Length', self.Length, 'uint32', false, errs, need_convert)

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

function TReadReq:unpack(_)
    return self.Offset, self.Length
end

FlashIO.ReadReq = TReadReq

FlashIO.interface = mdb.register_interface('bmc.kepler.Chip.FlashIO', {}, {
    Read = {'a{ss}uu', 'ay', TReadReq, TReadRsp},
    Write = {'a{ss}uay', '', TWriteReq, TWriteRsp},
    Erase = {'a{ss}uy', '', TEraseReq, TEraseRsp},
    RawRead = {'a{ss}uay', 'ay', TRawReadReq, TRawReadRsp},
    RawWrite = {'a{ss}ay', '', TRawWriteReq, TRawWriteRsp}
}, {})

return FlashIO
