-- 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 class = require('mc.class')
local stream = require('stream.stream')
local object_defs = require('object_defs')

local DEFAULT_SCAN_INTERVAL <const> = 200

local bus = class()

function bus:ctor(object)
    self.name = object.name
    self.stream = (stream[object.class_name] or stream.None).new(object, object.name)
    -- 未注册的器件类型按不需要扫描处理,避免object_defs查表失败
    local obj_def = object_defs[object.class_name] or {}
    self.need_scan = obj_def.need_scan or false
    self.scan_interval = obj_def.scan_interval or DEFAULT_SCAN_INTERVAL
end

function bus:set_high_task_cb(high_task_cb)
    self.high_task_cb = high_task_cb
end

function bus:notify_high_task(chip)
    local high_task_cb = self.high_task_cb
    if high_task_cb then
        high_task_cb(chip)
    end
end

function bus:set_secondary_task_cb(secondary_task_cb)
    self.secondary_task_cb = secondary_task_cb
end

function bus:notify_secondary_task(chip)
    local secondary_task_cb = self.secondary_task_cb
    if secondary_task_cb then
        secondary_task_cb(chip)
    end
end

function bus:write(input)
    self.stream:write(input)
end

function bus:read(input)
    return self.stream:read(input)
end

return bus