#include "ukccbluetoothconfig.h"

ukccbluetoothconfig::ukccbluetoothconfig()
{

}

void ukccbluetoothconfig::launchBluetoothServiceStart(const QString &processName)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QProcess *process = new QProcess();
    QString cmd = processName;
    //cmd.append(" -o");
    qDebug () << Q_FUNC_INFO << cmd << __LINE__;
    process->startDetached(cmd);
}

void ukccbluetoothconfig::killAppProcess(const quint64 &processId)
{
//    QString strCommand = "ps -ef|grep " + processName + " |grep -v grep |awk '{print $2}'";
//    QList <quint64> processId;
//    checkProcessRunning(processName,processId);
//    for(quint64 tempId : processId)
//    {
    qDebug () << Q_FUNC_INFO << "========================" << processId;
    QProcess *process = new QProcess();
    QString cmd = QString("kill -9 %1").arg(processId);
    process->startDetached(cmd);
//    }
}

bool ukccbluetoothconfig::checkProcessRunning(const QString &processName, QList<quint64> &listProcessId)
{
    qDebug() << Q_FUNC_INFO << __LINE__;

    bool res(false);
    QString strCommand;
    //if(processName == BluetoothServiceExePath)
    //    strCommand = "ps -ef|grep '" + processName + " -o' |grep -v grep |awk '{print $2}'";
    //else
        strCommand = "ps -ef|grep '" + processName + "' |grep -v grep |awk '{print $2}'";
    qDebug() << Q_FUNC_INFO << strCommand << __LINE__;

    QByteArray ba = strCommand.toLatin1();
    const char* strFind_ComName = ba.data();
    //const char* strFind_ComName = convertQString2char(strCommand);
    FILE * pPipe = popen(strFind_ComName, "r");
    if (pPipe)
    {
        std::string com;
        char name[512] = { 0 };
        while (fgets(name, sizeof(name), pPipe) != NULL)
        {
            int nLen = strlen(name);
            if (nLen > 0
                && name[nLen - 1] == '\n')
                //&& name[0] == '/')
            {
                name[nLen - 1] = '\0';
                listProcessId.append(atoi(name));
                res = true;
                break;
            }
        }
        pclose(pPipe);
    }
    return res;
}

static QString executeLinuxCmd(QString strCmd)
{
    QProcess p;
    p.start("bash", QStringList() <<"-c" << strCmd);
    p.waitForFinished();
    QString strResult = p.readAllStandardOutput();
    return strResult.toLower();
}


void ukccbluetoothconfig::setEnvPCValue()
{
    qDebug() << Q_FUNC_INFO <<__LINE__;
    unsigned int productFeatures = 0;
    char * projectName = "";
    char * projectSubName = "";
    productFeatures = kdk_system_get_productFeatures();
    projectName = kdk_system_get_projectName();
    QString qs_projectName = QString(projectName);
    projectSubName = kdk_system_get_projectSubName();
    QString qs_projectSubName = QString(projectSubName);

    envPC = Environment::NOMAL;
    QString str = executeLinuxCmd("cat /proc/cpuinfo | grep Hardware");
    if(str.length() == 0)
    {
        goto FuncEnd;
    }

    if(str.indexOf("huawei") != -1 || str.indexOf("pangu") != -1 ||
            str.indexOf("kirin") != -1)
    {
        envPC = Environment::HUAWEI;
    }

FuncEnd:
    if (QFile::exists("/etc/apt/ota_version"))
        envPC = Environment::LAIKA;
    else if(qs_projectName.contains("V10SP1-edu",Qt::CaseInsensitive) &&
            qs_projectSubName.contains("mavis",Qt::CaseInsensitive))
    {
        envPC = Environment::MAVIS;
    }

    qInfo () << Q_FUNC_INFO << envPC <<__LINE__;
}

