/*
 *
 *  kylin - Bluetooth configure for Linux
 *
 *  Copyright (C) 2023-  Kylin Corporation
 *  Copyright (C) 2023-  Dengtuo Kylin <dengtuo@kylinos.cn>
 *
 *
 */

#include "kylin_bluetooth.h"

bool is_huawei = false;
bool ctrl_plugin_flag = false;

void* ctrl_plugin_handle = NULL;
PluginInterface* ctrl_plugin_interface = NULL;

void btd_kylin_init(void)
{
    ctrl_plugin_flag = init_ctrl_plugin_so_file();
}

void btd_kylin_cleanup(void)
{
    close_ctrl_plugin_so_file();
}

bool systemEnvironmentinfo(void)
{
    FILE *fp;
    int nread=0;
    ssize_t len = 0;
    char *buf=NULL;
    char *buffer=NULL;
    char *file = FILE_CPUINFO;

    fp = fopen(file, "rb");
    if (fp == NULL)
    {
        printf("error to open: %s\n", file);
        fclose(fp);
        return false;
    }

    while((nread = getline(&buffer, &len, fp)) != -1) 
	{
        if((buf=strstr(buffer, "Hardware"))!=NULL && 
		((buf=strstr(buffer, "HUAWEI"))!=NULL || 
		 (buf=strstr(buffer, "Kirin" ))!=NULL ))  /* 简单实现grep的功能 */
		{
			fclose(fp);
			return true;
		}
	}
    fclose(fp);

    // 
    FILE *fp_dmidecode = popen("dmidecode |grep \"String 4\"", "r");
    if (fp_dmidecode == NULL) {
        printf("Failed to run command\n");
        return 1;
    }

    while (fgets(buffer, sizeof(buffer), fp_dmidecode) != NULL) {
        if((buf=strstr(buffer, "pgu"))!=NULL 
        || (buf=strstr(buffer, "PGU"))!=NULL 
        )  /* 简单实现grep的功能 */
		{
			int return_value = fclose(fp_dmidecode);
            printf("Return value: %d\n", return_value);
			return true;
		}
    }

    // 关闭输出流并获取命令的返回值
    int return_value = pclose(fp_dmidecode);
    printf("Return value: %d\n", return_value);

	return false;
}

bool init_ctrl_plugin_so_file()
{
	ctrl_plugin_handle = dlopen(CTRL_PLUGIN_SO_PATH, RTLD_LAZY);
    if (!ctrl_plugin_handle) {
        printf("[kylin log]Error loading plugin: %s\n", dlerror());
        return false;
    }

    ctrl_plugin_interface = dlsym(ctrl_plugin_handle, "plugin_interface");
    if (!ctrl_plugin_interface) {
        printf("[kylin log]Error loading plugin interface: %s\n", dlerror());
        return false;
    }
	return true;
}

unsigned int btd_kylin_dev_type(const char * dev_type_name)
{
    // 0：音频设备
    // 1：键鼠设备
    // 2：手机
    // 3：电脑
    // 4：其他
    //printf("dev_type_name:%s \n",dev_type_name);
    if (0 == g_strcmp0(dev_type_name,"audio-headset") || 0 == g_strcmp0(dev_type_name,"audio-headphones") || 0 == g_strcmp0(dev_type_name,"audio-card"))
        return 0;
    else if (0 == g_strcmp0(dev_type_name,"input-keyboard") || 0 == g_strcmp0(dev_type_name,"input-mouse"))
        return 1;
    else if (0 == g_strcmp0(dev_type_name,"phone") || 0 == g_strcmp0(dev_type_name,"input-tablet"))
        return 2;
    else if (0 == g_strcmp0(dev_type_name,"computer"))
        return 3;
    else
        return 4;
}

unsigned int btd_kylin_dev_ctrl(const char* dev_type_name,const char * dev_addr)
{
    //0 不管控 ， 1 管控
	unsigned int status = DEVICE_IS_NOT_CONTROLLED;
	if (ctrl_plugin_flag && NULL != ctrl_plugin_interface)
    {
        unsigned int dev_type = btd_kylin_dev_type(dev_type_name);
		status = ctrl_plugin_interface->bluetooth_dev_control(dev_type,dev_addr);
    }
    //printf("dev_type_name:%s ,dev_addr:%s ,status: %d\n",dev_type_name,dev_addr,status);
	return status;
}



void close_ctrl_plugin_so_file()
{
	if (ctrl_plugin_flag && ctrl_plugin_handle)
		dlclose(ctrl_plugin_handle);
}
