-- 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 logging = require('mc.logging')
local ret = pcall(function()
    local fix = require('plugins.fix')
    --执行热修复
    fix.load_all_patches('hwproxy')
end)
if ret then
    logging:notice('hwproxy load patches successfully')
end

local skynet = require('skynet')
require('skynet.manager')
local hwproxy_app = require('hwproxy_app')
local health = require('hwproxy_health')
local app

local function change_debuglog_path(option)
    if not option then
        return
    end

    logging:setLevel(logging.INFO)
    logging.info = function(self, fmt, ...)
        skynet.error('INFO', string.format(fmt, ...))
    end
    logging.error = function(self, fmt, ...)
        skynet.error('ERROR', string.format(fmt, ...))
    end
    logging.notice = function(self, fmt, ...)
        skynet.error('NOTICE', string.format(fmt, ...))
    end
end

change_debuglog_path(false)

local CMD = {}

function CMD.exit()
    logging:mcf_info('hwproxy service exit')
end

function CMD.update_resource(...)
    app:update_resource(...)
end

function CMD.object_register(...)
    app:object_register(...)
end

function CMD.restart_work_service(...)
    return app:restart_work_service(...)
end

function CMD.get_bus_object(...)
    return app:get_bus_object(...)
end

function CMD.read_write_func(...)
    return app:read_write_func(...)
end

function CMD.accessor_operate(...)
    return app:accessor_operate(...)
end

function CMD.set_health_status_abn(...)
    logging:notice('set hwproxy service health status need to restart')
    health.set_restart_now()
end

function CMD.trace_chip(...)
    return app:trace_chip(...)
end

skynet.start(function()
    skynet.uniqueservice('sd_bus')
    skynet.register('.hwproxy')
    app = hwproxy_app.new()
    health.callback_register()
    local ok, handler = pcall(require, 'hwproxy_manufacture_app')
    if ok then
        local manufacture = handler.new(app)
        manufacture:init()
    end
    skynet.dispatch('lua', function(_, _, cmd, ...)
        local f = assert(CMD[cmd])
        skynet.ret(skynet.pack(f(...)))
    end)
end)