/*
 * libkysdk-system's Library
 *
 * Copyright (C) 2023-2025, KylinSoft Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: ShaoShuai Tian <tianshaoshuai@kylinos.cn>
 *
 */

/**
 * @file CpuMethod.java
 * @author tianshaoshuai (tianshaoshuai@kylinos.cn)
 * @brief Java获取cpu信息
 * @version 0.1
 * @date 2023-2-17
 * 
 * @copyright Copyright (c) 2021
 * @defgroup libkysdk-system-java
 * @{
 * 
 */

package kylin.kysdk.java.method;

import kylin.kysdk.java.client.DBusClient;
import lombok.extern.slf4j.Slf4j;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.types.UInt32;
import com.kylin.kysdk.cpuinfo;

/**
 * @brief 获取cpu信息类
 * 主要用来获取cpu的架构、制造厂商、型号、额定主频、核心数量、虚拟化支持和线程数信息
 */
@Slf4j
public class CpuMethod implements cpuinfo{
    /**
     * @brief 获取cpu架构
     *
     * @return String 架构信息，如"x86_64"
     */
    @Override
    public String getCpuArch(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            cpuinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/cpuinfo", cpuinfo.class);
            return obj.getCpuArch();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException getCpuArch:", e);
        }
        return null;
    }

    /**
     * @brief 获取cpu制造厂商
     *
     * @return String 制造厂商，如“GenuineIntel”
     */
    @Override
    public String getCpuVendor(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            cpuinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/cpuinfo", cpuinfo.class);
            return obj.getCpuVendor();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException getCpuVendor:", e);
        }
        return null;
    }

    /**
     * @brief 获取cpu型号
     *
     * @return String CPU型号名称，如“Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz”
     */
    @Override
    public String getCpuModel(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            cpuinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/cpuinfo", cpuinfo.class);
            return obj.getCpuModel();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException getCpuModel:", e);
        }
        return null;
    }

    /**
     * @brief 获取cpu额定主频
     *
     * @return String 额定主频赫兹数，如“1794.742”
     */
    @Override
    public String getCpuFreqMHz(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            cpuinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/cpuinfo", cpuinfo.class);
            return obj.getCpuFreqMHz();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException getCpuFreqMHz:", e);
        }
        return null;
    }

    /**
     * @brief 获取cpu核心数量
     *
     * @return UInt32 所有可用的CPU核心数量
     */
    @Override
    public UInt32 getCpuCorenums(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            cpuinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/cpuinfo", cpuinfo.class);
            return obj.getCpuCorenums();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException getCpuCorenums:", e);
        }
        return null;
    }

    /**
     * @brief 获取cpu对虚拟化的支持
     *
     * @return String 若CPU支持虚拟化，则返回虚拟化技术，如“vmx”；若不支持，返回NULL
     */
    @Override
    public String getCpuVirt(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            cpuinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/cpuinfo", cpuinfo.class);
            return obj.getCpuVirt();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException getCpuVirt:", e);
        }
        return null;
    }

    /**
     * @brief 获取cpu线程数
     *
     * @return UInt32 cpu支持的线程数
     */
    @Override
    public UInt32 getCpuProcess(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            cpuinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/cpuinfo", cpuinfo.class);
            return obj.getCpuProcess();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException getCpuProcess:", e);
        }
        return null;
    }

    /**
     * @brief 判断是否为远程对象
     *
     * @return boolean Returns true on remote objects. Local objects implementing this interface MUST return false.
     */
    @Override
    public boolean isRemote() {
        return false;
    }

    /**
     * @brief 获取ObjectPath【未实现】
     *
     * @return String ObjectPath
     */
    @Override
    public String getObjectPath() {
        return null;
    }
}

/**
 * \example kysdk-system/src/kysdk-java/kysdk-java-ext/kysdk-java/src/test/java/kylin/kysdk/java/EventTest.java
 * 
 */
