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

---@class GPIO.ReadRsp
---@field Result integer
local TReadRsp = {}
TReadRsp.__index = TReadRsp
TReadRsp.group = {}

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

function TReadRsp.new(Result)
    return TReadRsp_from_obj({Result = Result})
end
---@param obj GPIO.ReadRsp
function TReadRsp:init_from_obj(obj)
    self.Result = obj.Result
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 = {'Result'}

TReadRsp.default = {0}

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

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

    validate.Optional(prefix .. 'Result', self.Result, '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.Result
end

GPIO.ReadRsp = TReadRsp

---@class GPIO.ReadReq
---@field ID integer
---@field Direction integer
local TReadReq = {}
TReadReq.__index = TReadReq
TReadReq.group = {}

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

function TReadReq.new(ID, Direction)
    return TReadReq_from_obj({ID = ID, Direction = Direction})
end
---@param obj GPIO.ReadReq
function TReadReq:init_from_obj(obj)
    self.ID = obj.ID
    self.Direction = obj.Direction
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 = {'ID', 'Direction'}

TReadReq.default = {0, 0}

TReadReq.struct = {{name = 'ID', is_array = false, struct = nil}, {name = 'Direction', is_array = false, struct = nil}}

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

    validate.Optional(prefix .. 'ID', self.ID, 'uint32', false, errs, need_convert)
    validate.Optional(prefix .. 'Direction', self.Direction, 'uint8', 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.ID, self.Direction
end

GPIO.ReadReq = TReadReq

GPIO.interface = mdb.register_interface('bmc.kepler.Managers.SOC.GPIO', {},
    {Read = {'a{ss}uy', 'y', TReadReq, TReadRsp}}, {ACLost = 'a{ss}u'})

return GPIO
