-- 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 utils = require('mc.utils')
local vos = require('utils.vos')
local expression = require('mc.class_mgnt.expression')
local c_tasks = require('mc.orm.tasks')
local skynet = require('skynet')
local syslog_core = require('syslog.core')
local class = require('mc.class')
local log = require('mc.logging')
local object_pool = require('object_pool')
local hw_utils = require('hw_utils.utils')
local timestamp_table = require('hw_utils.timestamp_table')
local g_expr = expression.new()

local STATUS_STOPPED <const> = 'stopped'
local STATUS_RUNNING <const> = 'running'
local STATUS_IDLE <const> = 'idle'
local DEFAULT_MASK <const> = 0xffffffff
local BLOCK_IO_TYPE <const> = 1

local BYTE_MAX_MASK <const> = 0xff
local SMC_VERSION_OPCODE <const> = 0x900
local SMC_VERSION_BYTE <const> = 1
local DFX_PERIOD_DELAY_TIME <const> = 30000

local s_pack = string.pack
local s_unpack = string.unpack
local s_format = string.format
local s_byte = string.byte
local s_sub = string.sub
local t_concat = table.concat
local t_insert = table.insert
local math_ceil = math.ceil
local object_pool_new = object_pool.new
local get_uptime = hw_utils.get_uptime
local getLevel = log.getLevel
local LOG_DEBUG = log.DEBUG
local get_value_with_mask = utils.get_value_with_mask

local CONTEXT_FIELD <const> = { 'SmcVersion' }
local AGGREGATE_TYPE_SMC <const> = 'Smc'
local DUMP_RESULT_LIMIT <const> = 10

local LOG_LIMIT_COUNT = 20
local LOG_LIMIT_PERIOD = 24 * 3600 -- 24 hours

local at_start_up = true
skynet.timeout(DFX_PERIOD_DELAY_TIME, function()
    at_start_up = false
end)

local aggregate = class()

local function save_config_and_mapping(self, object)
    local config = {}
    self.config = config
    for index, byte_data in pairs(object.Config or {}) do
        for hw_name, mask in pairs(byte_data) do
            config[hw_name] = { index = index, mask = mask }
        end
    end

    self.mapping = object.Mapping
    self.critical_bytes = {}
    if object.CriticalByteIndexes then
        log:notice('%s.CriticalByteIndexes =  %s', self.name, object.CriticalByteIndexes)
    end

    for _, index in pairs(object.CriticalByteIndexes or {}) do
        self.critical_bytes[index] = true
    end
end

local function make_byte_value_func(aggregate_instance)
    return function(hw_name)
        return aggregate_instance:get_byte_value(hw_name)
    end
end

function aggregate:ctor(chip_obj, object, aggregate_type)
    self.name = object.name
    self.chip = chip_obj
    self.offset = object.Offset or object.BaseOffset
    self.size = object.Size or object.Length
    self.period = object.Period > 0 and object.Period or 1000
    self.aggregate_type = aggregate_type
    self.result = nil
    self.dump_results = timestamp_table.new(DUMP_RESULT_LIMIT)
    self.result_timestamp = nil
    self.sys_time = nil
    self.error = nil
    self.status = STATUS_IDLE
    self.stopped = false

    self.byte_value_getter = make_byte_value_func(self)

    if aggregate_type == AGGREGATE_TYPE_SMC then
        self.version = object.SmcVersion
        save_config_and_mapping(self, object)
        self.version_valid = false
        self.context = setmetatable({}, {
            __index = function(_, context_name)
                if not self.result or not object.Context[context_name] then
                    return nil
                end

                return g_expr:get_expressions_result({ object.Context[context_name] }, function(hw_name)
                    return self:get_byte_value(hw_name)
                end)
            end,
        })

        -- Smc版本号来源1.通过命令字查询,2. 通过Dfx第一个byte获取,3. 通过context中的表达式获取
        self.smc_version_by_opcode = nil
        self.smc_version_by_dfx = nil
        self.smc_version_by_expr = nil
    end

    self.second_wheel = {}
    self.m_second_wheel = {}
    self.cur_second_slot = 1
    self.cur_m_second_slot = 1

    self.log_count = 0
    self.omit_count = 0
    self.log_period_start = nil

    log:notice(
        'create %s aggregate success, offset: %s, period: %s, length: %s',
        self.name,
        self.offset,
        self.period,
        self.size
    )
end

function aggregate:init()
    for i = 1, 100 do
        self.second_wheel[i] = {}
    end

    local m_second_slot_count
    local m_period = self.period % 1000
    if m_period == 0 then
        m_second_slot_count = 1
    else
        m_second_slot_count = math_ceil(1000 / m_period)
    end
    for i = 1, m_second_slot_count do
        self.m_second_wheel[i] = {}
    end

    local fetch = { result = nil, timeout = false, error = nil }
    fetch.cb = function()
        self:save_result(fetch)
    end

    fetch.run_again = function()
        self.status = STATUS_IDLE
        self:start_task()
    end

    self.fetch = fetch
    self:start()
end

function aggregate:second_task_execute()
    local cur_second_slot = self.cur_second_slot
    local second_wheel = self.second_wheel
    local m_second_wheel = self.m_second_wheel
    for _, task in ipairs(second_wheel[cur_second_slot]) do
        t_insert(m_second_wheel[task.expect_m_second_slot], task.scanner)
    end
    second_wheel[cur_second_slot] = {}
end

function aggregate:add_scanner(scanner)
    local tick_count = math_ceil(scanner.adjust_period / self.period)
    local expect_m_second_slot = (self.cur_m_second_slot + tick_count - 1) % #self.m_second_wheel + 1
    local expect_second_slot = (
        self.cur_second_slot
        + (self.cur_m_second_slot + tick_count - 1) // #self.m_second_wheel
        - 1
    )
            % #self.second_wheel
        + 1

    if expect_second_slot == self.cur_second_slot then
        table.insert(self.m_second_wheel[expect_m_second_slot], scanner)
    else
        table.insert(
            self.second_wheel[expect_second_slot],
            { scanner = scanner, expect_m_second_slot = expect_m_second_slot }
        )
    end
end

local function get_scanner_value(self, scanner)
    local value_func = self.aggregate_type == AGGREGATE_TYPE_SMC and self.get_mapping_value or self.get_single_value
    local match, ok, result, consume_time = value_func(self, scanner)
    if not match then
        return false
    end

    local rsp = { result = ok and result or false, consume_time = consume_time }
    if not ok then
        rsp.error = result
    end
    return true, rsp
end

function aggregate:m_second_task_execute()
    local cur_m_second_slot = self.cur_m_second_slot
    if cur_m_second_slot == 1 then
        self:second_task_execute()
    end

    local m_second_wheel = self.m_second_wheel
    local tasks = m_second_wheel[cur_m_second_slot]
    m_second_wheel[cur_m_second_slot] = {}
    local add_scanner = self.add_scanner
    for _, scanner in ipairs(tasks) do
        -- 当scanner的resources为空的时候,不做处理
        if #scanner.resources == 0 then
            goto continue
        end
        local ok, ret, rsp = pcall(get_scanner_value, self, scanner)
        if not ok or not ret then
            if not ok then
                log:error('scanner %s get scanner value failed. error: %s ', scanner.name, ret)
            else
                log:hw_stream_error(
                    'scanner: %s retrieve value from aggregate failed,'
                        .. ' and it will no longer be updated by aggregate',
                    scanner.name
                )
            end
            scanner:set_in_aggregate(false)
            scanner:start()
            goto continue
        end

        if getLevel(log) == LOG_DEBUG then
            local hex_str = type(rsp.result) == 'string' and utils.to_hex(rsp.result) or false
            log:debug(
                'aggregate: %s update scanner: %s, result: %s, error: %s',
                self.name,
                scanner.name,
                hex_str,
                ok and 'nil' or rsp.result
            )
        end
        ok, ret = pcall(scanner.reply_from_dfx, scanner, rsp)
        if not ok then
            log:error('scanner %s update value failed. error: %s', scanner.name, ret)
        end
        add_scanner(self, scanner)
        ::continue::
    end
    local next_m_second_slot = cur_m_second_slot + 1
    if next_m_second_slot > #m_second_wheel then
        self.cur_m_second_slot = 1
        local next_second_slot = self.cur_second_slot + 1
        if next_second_slot > #self.second_wheel then
            self.cur_second_slot = 1
        else
            self.cur_second_slot = next_second_slot
        end
    else
        self.cur_m_second_slot = next_m_second_slot
    end
end

function aggregate:add_dfx_config(object)
    if self.aggregate_type ~= AGGREGATE_TYPE_SMC then
        return
    end
    self.period = math.min(self.period, object.Period)
    self.size = math.max(self.size, object.Size)
    for index, byte_data in pairs(object.Config) do
        for hw_name, mask in pairs(byte_data) do
            if not self.config[hw_name] then
                self.config[hw_name] = { index = index, mask = mask }
            end
        end
    end

    for scanner_name, mapping in pairs(object.Mapping) do
        self.mapping[scanner_name] = mapping
    end

    for _, index in pairs(object.CriticalByteIndexes or {}) do
        self.critical_bytes[index] = true
    end
end

function aggregate:start()
    if self.status == STATUS_RUNNING then
        return
    end
    if self.chip.host_bus and self.chip.host_bus.need_scan then
        if self.aggregate_type == AGGREGATE_TYPE_SMC then
            -- 通过命令字直接查询CPLD版本号
            local fetch = { result = nil, timeout = false }
            fetch.cb = function()
                self:save_smc_version(fetch)
                -- 查询完版本号后再查询DfxInfo
                log:notice('aggregate: %s start to get SMC dfx info', self.name)
            end
            fetch.run_again = function()
                self.status = STATUS_IDLE
                self:start_task()
            end
            local read_args = object_pool_new(
                'read_args',
                SMC_VERSION_OPCODE,
                SMC_VERSION_BYTE,
                DEFAULT_MASK,
                DEFAULT_MASK,
                1,
                fetch,
                nil,
                nil,
                nil,
                self.name
            )
            self.chip.low_cache:read(read_args)
        else
            log:notice('aggregate: %s, start to get aggregate data', self.name)
            self:start_task()
        end
    end
end

function aggregate:start_task()
    if self.status == STATUS_RUNNING or self.stopped then
        return
    end

    self:cancel_timer()
    self.status = STATUS_RUNNING
    local period = self.period
    if at_start_up then
        period = period * 3 > 3000 and 3000 or period * 3
    end

    local offset, fetch, name, low_cache = self.offset, self.fetch, self.name, self.chip.low_cache
    local low_cache_read = low_cache.read
    self.timer = c_tasks:get_instance():timeout_ms(period, function()
        local read_args = object_pool_new(
            'read_args',
            offset,
            self.size,
            DEFAULT_MASK,
            DEFAULT_MASK,
            BLOCK_IO_TYPE,
            fetch,
            nil,
            nil,
            nil,
            name
        )

        local ok, ret = pcall(low_cache_read, low_cache, read_args)
        if not ok then
            log:error('fail to read values from the aggregated object %s, err: %s', name, ret)
            self.status = STATUS_STOPPED
            self:start_task()
        end
        self.timer = nil
        return false
    end)
end

local function get_smc_version_trans(smc_version)
    return (smc_version >> 4) * 100 + (smc_version & 0xf)
end

function aggregate:save_smc_version(fetch)
    if fetch.result then
        local smc_version = s_byte(fetch.result, SMC_VERSION_BYTE)
        self.smc_version_by_opcode = get_smc_version_trans(smc_version)
        log:notice('smc_dfx_info: %s, get smc version by opcode: %s', self.name, self.smc_version_by_opcode)
    end
end

function aggregate:cancel_timer()
    if self.timer then
        self.timer:cancel()
        self.timer = nil
    end
end

function aggregate:stop()
    self.status = STATUS_STOPPED
    self.stopped = true
    self:cancel_timer()
    log:notice('aggregate: %s stop getting SMC dfx info', self.name)
end

-- 检查SMC是否支持DfxInfo
function aggregate:check_version_valid()
    -- 优先级顺序:表达式配置的版本号 > 通过命令字获取的版本号 > 通过Dfx获取的版本号
    local version = self.smc_version_by_expr or self.smc_version_by_opcode or self.smc_version_by_dfx
    if self.version > version then
        -- 版本检查失败
        return false
    else
        self.version_valid = true
    end
    return true
end

function aggregate:log_with_count(cache_data)
    local now = hw_utils.get_uptime()
    if not self.log_period_start or (now - self.log_period_start >= LOG_LIMIT_PERIOD) then
        self.log_count = 0
        self.log_period_start = now
    end

    if self.log_count >= LOG_LIMIT_COUNT then
        self.omit_count = self.omit_count + 1
        return
    end

    self.log_count = self.log_count + 1
    local omitted = self.omit_count
    self.omit_count = 0

    log:notice(
        'aggregate %s critical byte change (log %s/%s%s), data: %s',
        self.name,
        self.log_count,
        LOG_LIMIT_COUNT,
        omitted > 0 and ', omitted ' .. omitted .. ' changes' or '',
        utils.to_hex(cache_data)
    )
end

function aggregate:log_critical_change(cache_result)
    if not (self.critical_bytes and next(self.critical_bytes) ~= nil) then
        return
    end
    if not self.result then
        log:hw_stream_error('aggregate %s read failed, error: %s', self.name, cache_result)
        return
    end

    local last_valid = self.dump_results:get_latest_valid()
    for index, _ in pairs(self.critical_bytes) do
        if last_valid and cache_result and s_byte(cache_result, index) ~= s_byte(last_valid, index) then
            self:log_with_count(cache_result)
            break
        end
    end
end

function aggregate:process_smc_result()
    if not self.smc_version_by_dfx and self.result then
        -- 通过表达式配置的版本号
        self.smc_version_by_expr = self.context.SmcVersion
        -- 通过Dfx第一个byte获取的版本号
        local smc_version = s_byte(self.result, SMC_VERSION_BYTE)
        self.smc_version_by_dfx = get_smc_version_trans(smc_version)
    end

    if not self.version_valid and self.result then
        local valid = self:check_version_valid()
        if not valid then
            log:error(
                'aggregate: %s stop reading, check smc version failed, expect version: %s, version by expr: %s,\z
                        version by opcode: %s, version by dfx: %s',
                self.name,
                self.version,
                self.smc_version_by_expr,
                self.smc_version_by_opcode,
                self.smc_version_by_dfx
            )
            -- 版本号检查失败,不再继续读取DfxInfo
            self.chip:exit_smc_dfx_info()
            self.fetch.run_again = nil
            return false
        end
        log:notice(
            'aggregate: %s, version check success, expect version: %s, version by expr: %s, version by \z
                        opcode: %s, version by dfx: %s',
            self.name,
            self.version,
            self.smc_version_by_expr,
            self.smc_version_by_opcode,
            self.smc_version_by_dfx
        )
    end
end

function aggregate:process_result(fetch)
    self.result = fetch.result
    local cache_result = self.result or fetch.error
    local now_time = get_uptime()
    self.result_timestamp = now_time
    local time = syslog_core.get_log_time_str(syslog_core.LOG_US_TIME)
    self.sys_time = time
    local cache_data = self.dump_results:get_latest()
    if cache_data ~= cache_result then
        local ok, err = pcall(self.log_critical_change, self, cache_result)
        if not ok then
            log:error('log_critical_change failed: %s', err)
        end
        if now_time == -1 or not self.dump_results:add(now_time, cache_result, self.result, time) then
            log:debug('failed to insert smcdfx result into table')
        end
    end
    self.error = fetch.error
    self.consume_time = fetch.consume_time

    fetch.result = nil
    fetch.timeout = false
    fetch.error = nil
    fetch.consume_time = nil
    self.status = STATUS_IDLE
    if self.aggregate_type == AGGREGATE_TYPE_SMC then
        self:process_smc_result()
    end

    if getLevel(log) == LOG_DEBUG then
        local hex_str = type(self.result) == 'string' and utils.to_hex(self.result) or false
        log:debug('aggregate: %s get result: %s, self.error: %s', self.name, hex_str, self.error)
    end

    self:m_second_task_execute()
    return true
end

function aggregate:save_result(fetch)
    local ok, ret = pcall(self.process_result, self, fetch)
    if not ok then
        log:hw_stream_error('aggregate: %s, processes result failed, error: %s', self.name, ret)
    end

    local current_time = vos.vos_tick_get()
    if self.start_time == nil then
        -- 首次和末次更新时间戳
        self.start_time, self.end_time = current_time, current_time

        -- 更新次数
        self.count = 0

        -- 最长更新周期
        self.max_single_update_period = 0
    else
        local time_distance = current_time - self.end_time
        self.end_time = current_time
        self.max_single_update_period = math.max(time_distance, self.max_single_update_period)
        self.count = self.count + 1
    end
end

function aggregate:get_byte_value(hw_name)
    local config = self.config[hw_name]

    local byte_value = s_byte(self.result, config.index)
    if config.mask == BYTE_MAX_MASK then
        return byte_value
    end
    return utils.get_value_with_mask(config.mask, byte_value)
end

function aggregate:get_mapping_value(scanner)
    local scanner_name = scanner.resources[1].name
    local t_mapping = self.mapping
    for _, res in ipairs(scanner.resources) do
        if t_mapping[res.name] then
            scanner_name = res.name
        end
    end
    local length = scanner.size
    if not self.version_valid then
        return false
    end
    if not t_mapping[scanner_name] or self.result == nil then
        return false
    end

    if self.result == false then
        return true, false, self.error
    end

    local expr = t_mapping[scanner_name].Value or t_mapping[scanner_name].value

    local value = g_expr:get_expressions_result({ expr }, self.byte_value_getter)

    if value == nil then
        return false
    end

    return true, true, s_pack(s_format('I%d', length), value), self.consume_time
end

function aggregate:get_single_value(scanner)
    if not self.result then
        return true, false, self.error
    end

    local result = self.result
    local offset = scanner.aggregate_offset
    if offset >= #result then
        return true, false, 'aggregate offset is incorrect'
    end

    local length = scanner.size
    if scanner.type == BLOCK_IO_TYPE then
        return true, true, s_sub(result, offset + 1, offset + length), self.consume_time
    end

    local format = 'I' .. tostring(length)
    local value = s_unpack(format, result, offset + 1)
    return true, true, s_pack(format, get_value_with_mask(scanner.mask, value)), self.consume_time
end

function aggregate:dump()
    local t_dump_str = {
        s_format('Name: %s\n', self.name),
        s_format('Offset: 0x%x\n', self.offset),
        s_format('Size: %d\n', self.size),
        s_format('Period: %dms\n', self.period),
    }

    if self.aggregate_type == AGGREGATE_TYPE_SMC then
        t_dump_str[#t_dump_str + 1] = s_format('SmcVersion: %s\n', self.version)
        t_dump_str[#t_dump_str + 1] = 'Config: \n'
        t_dump_str[#t_dump_str + 1] = s_format('\t%-50s\tindex\tmask\n', 'hw_name')
        for hw_name, config in pairs(self.config) do
            t_dump_str[#t_dump_str + 1] = s_format('\t%-50s\t%s\t0x%x\n', hw_name, config.index, config.mask)
        end

        t_dump_str[#t_dump_str + 1] = 'Mapping: \n'
        t_dump_str[#t_dump_str + 1] = s_format('\t%-50s\tvalue\n', 'scanner_name')
        for scanner_name, mapping in pairs(self.mapping) do
            t_dump_str[#t_dump_str + 1] = s_format('\t%-50s\t%s\n', scanner_name, mapping.Value or mapping.value)
        end

        t_dump_str[#t_dump_str + 1] = 'Context: \n'
        for _, field in ipairs(CONTEXT_FIELD) do
            t_dump_str[#t_dump_str + 1] = s_format('\t%-50s\t%s\n', field, self.context[field])
        end
        t_dump_str[#t_dump_str + 1] = s_format('VersionByOpcode: %s\n', self.smc_version_by_opcode)
    end

    local results = self.dump_results:get_all_result()
    if results then
        for _, value in ipairs(results) do
            if value[2] and not string.find(value[2], 'failed:') then
                t_dump_str[#t_dump_str + 1] = s_format(
                    'TimeStamp: %s s\nSystemTime: %s\nData: \n%s\n',
                    value[1],
                    value[3],
                    utils.to_hex(value[2])
                )
            else
                t_dump_str[#t_dump_str + 1] =
                    s_format('TimeStamp: %s s\nSystemTime: %s\nData: \n%s\n', value[1], value[3], value[2])
            end
        end
    end

    if not self.result then
        t_dump_str[#t_dump_str + 1] =
            s_format('TimeStamp: %s s \nSystemTime: %s\nError: %s\n', self.result_timestamp, self.sys_time, self.error)
    else
        t_dump_str[#t_dump_str + 1] = s_format(
            'TimeStamp: %s s\nSystemTime: %s\nData: %s\n',
            self.result_timestamp,
            self.sys_time,
            utils.to_hex(self.result)
        )
    end
    if self.count ~= 0 then
        t_dump_str[#t_dump_str + 1] = s_format(
            'Average update period: %.3f ms, Maximum single update period: %s ms\n',
            (self.end_time - self.start_time) / self.count,
            self.max_single_update_period
        )
    end
    t_dump_str[#t_dump_str + 1] = '\n'
    return t_concat(t_dump_str)
end

return aggregate