/*
 * Copyright 2023 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "upmcpucoreinfo.h"

#include "../upmcommonfile.h"
#include "../upmcustomhardwareconfig.h"

UpmCpuCoreInfo::UpmCpuCoreInfo(int cpuCoreId, QObject *parent) : QObject(parent)
{
    m_cpuCoreId = cpuCoreId;

    QString result;
    if (true == UpmCommonFile::read(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                                    CPU_SCALING_AVAILABLE_GOVERNORS, result)) {
        if(UpmCustomHardwareConfig::self()->isD3000M()) {
            m_coreFreqMode[Balance] = "schedutil";
        } else {
            if (result.contains("ondemand")) {
                m_coreFreqMode[Balance] = "ondemand";
            } else if (result.contains("schedutil")) {
                m_coreFreqMode[Balance] = "schedutil";
            }
        }
    }

    if (true == UpmCommonFile::read(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                                    CPU_SCALING_MAX_FREQ, result)) {
        m_defaultMaxFreq = result.toUInt();
    }

    if (true == UpmCommonFile::read(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                                    CPU_SCALING_MIN_FREQ, result)) {
        m_defaultMinFreq = result.toUInt();
    }
}

int UpmCpuCoreInfo::getCpuCoreOnlineState()
{
    QString result;
    if (true == UpmCommonFile::read(QString(CPU_DEVICES_PATH).arg(m_cpuCoreId),
                                    CPU_CORE_ONLINE, result)) {
        return result.toInt();
    }
    return cpu_core_error;
}

void UpmCpuCoreInfo::setCpuCoreOnlineState(int online)
{
    if (cpu_core_online == online || cpu_core_offline == online) {
        UpmCommonFile::write(QString(CPU_DEVICES_PATH).arg(m_cpuCoreId),
                             CPU_CORE_ONLINE, QString::number(online));
    }
}

void UpmCpuCoreInfo::setCorePolicy(int cpuPolicy)
{
    if (cpuPolicy < Performance || cpuPolicy > Powersave) {
        return ;
    }
    UpmCommonFile::write(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                         CPU_SCALING_GOVERNORS,
                         m_coreFreqMode[cpuPolicy]);
}

void UpmCpuCoreInfo::setCoreMaxFreq(int cpuFrequency)
{
    if (cpuFrequency > m_defaultMaxFreq) {
        UpmCommonFile::write(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                             CPU_SCALING_MAX_FREQ, QString::number(m_defaultMaxFreq));
    } else if (cpuFrequency < m_defaultMinFreq) {
        UpmCommonFile::write(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                             CPU_SCALING_MAX_FREQ, QString::number(m_defaultMinFreq));
    } else {
        UpmCommonFile::write(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                             CPU_SCALING_MAX_FREQ, QString::number(cpuFrequency));
    }
}

int UpmCpuCoreInfo::getDefaultCoreMaxFreq()
{
    return m_defaultMaxFreq;
}

void UpmCpuCoreInfo::setDefaultCoreMaxFreq()
{
    UpmCommonFile::write(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                         CPU_SCALING_MAX_FREQ, QString::number(m_defaultMaxFreq));
}

QString UpmCpuCoreInfo::getCpuCorePolicy()
{
    QString result;
    UpmCommonFile::read(QString(CPU_DEVICES_FREQ_PATH).arg(m_cpuCoreId),
                        CPU_SCALING_GOVERNORS, result);
    return result;
}
