
/******************************************************************************
*版权所有(C) 2024 麒麟软件有限公司
*文 件 名 :kylin_cfgInfo.c
*作	 者 :马强
*内容摘要 :
*创建日期 :2024年11月17日
*修改记录1:
*修 改 人 :
*修改日期 :
*修改内容 :
******************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>  
#include <string.h>  
#include <pthread.h>
#include <fcntl.h>
#include <sys/prctl.h>

#include "src/core/nm-default-daemon.h"
#include "libnm-glib-aux/nm-dbus-aux.h"
#include "libnm-core-impl/nm-default-libnm-core.h"
#include "kylin_common.h"
#include "kylin_component.h"

#define KYLIN_FLIGHT_DBUS_SERVICE       "com.kylin.ukui.SettingsDaemon"
#define KYLIN_FLIGHT_DBUS_PATH          "/globalconfig"
#define KYLIN_FLIGHT_DBUS_INTERFACE     "com.kylin.ukui.SettingsDaemon.interface"

#define KYLIN_FL_DBUS_MAX_TIMEMS (3000) //超时时间3s

typedef struct ST_KylinFlightInfor 
{
	GDBusProxy *proxy;
	void *cbUserData;
	flightStateChangeCb funcFlightChangeCb;
	int isSupport;
	bool bindFlight;
	int flightState;
}st_KylinFlightInfor;

st_KylinFlightInfor gKylinFlightInfor;


// 信号处理函数
static void flightModeChangeCb(GDBusProxy *proxy,int newState, gpointer user_data) 
{
	st_KylinFlightInfor *pKylinFlightInfor=(st_KylinFlightInfor*)user_data;

	if(!pKylinFlightInfor) {
		KYLIN_COMMON_LOG(_LOGK_ERR, "user date is null,maybe not init!");
		return;
	}

	KYLIN_COMMON_LOG(_LOGK_INFO, " change flight state from:%d to: %d!",pKylinFlightInfor->flightState,newState);
	pKylinFlightInfor->flightState=newState;

	if(pKylinFlightInfor->funcFlightChangeCb){
		pKylinFlightInfor->funcFlightChangeCb(pKylinFlightInfor->cbUserData,newState);
	}
	return ;
}


static void dbusReadyCb (GObject *object,GAsyncResult *res,st_KylinFlightInfor *pKylinFlightInfor)
{
	GError *error = NULL;

	pKylinFlightInfor->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
	if (!pKylinFlightInfor->proxy ) 
	{
		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
			KYLIN_COMMON_LOG (_LOGK_ERR,"failed to acquire D-Bus proxy: %s",error->message);
		g_clear_error (&error);
		return;
	}


	_nm_dbus_proxy_signal_connect (pKylinFlightInfor->proxy, "FlightModeChanged", G_VARIANT_TYPE ("(i)"),
	                                          G_CALLBACK (flightModeChangeCb), pKylinFlightInfor);
	KYLIN_COMMON_LOG(_LOGK_INFO, "kylin flight dbus ready!");


}


int kylinGetFlightModeStateDbus()
{
	GDBusProxy *proxy=NULL;
	GVariant *result;
	GError *error = NULL;
	GVariant *modValue=NULL;
	void *backValue=NULL;
	int res =KYLIN_FLIGHT_ERR;

	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
	G_DBUS_PROXY_FLAGS_NONE,NULL,
											KYLIN_FLIGHT_DBUS_SERVICE,	  
											KYLIN_FLIGHT_DBUS_PATH, 						 
											KYLIN_FLIGHT_DBUS_INTERFACE, 
										   NULL, &error);

	
	if(!proxy) return NULL;
	result = g_dbus_proxy_call_sync (proxy,
								    "GetFlightModeState",
								  NULL,
								  G_DBUS_CALL_FLAGS_NONE,KYLIN_FL_DBUS_MAX_TIMEMS,
								  NULL, &error);
	if (!result) 
	{
		g_dbus_error_strip_remote_error (error);
		KYLIN_COMMON_LOG (_LOGK_ERR,"Error send log failed,%s", error->message);
		g_clear_error (&error);
		goto out;
	}

#if 0
	if(g_variant_is_of_type(result, G_VARIANT_TYPE_INT32)){
		g_variant_get(result, "(i)",&res );
		KYLIN_COMMON_LOG(_LOGK_DEBUG, "recv back %d!",res);
	}else{
		KYLIN_COMMON_LOG(_LOGK_DEBUG, "!");	
	}
#endif
	g_variant_get(result, "(i)",&res );
	KYLIN_COMMON_LOG(_LOGK_DEBUG, "recv back %d!",res);


	out:
	if (result) g_variant_unref (result);
	g_object_unref (proxy);

	return res;
}

int kylinFlightComponentInit(void  * cbUserData,flightStateChangeCb changeCb)
{

	memset(&gKylinFlightInfor,0,sizeof(st_KylinFlightInfor));
	
	gKylinFlightInfor.funcFlightChangeCb=changeCb;
	gKylinFlightInfor.cbUserData=cbUserData;
	gKylinFlightInfor.flightState=kylinGetFlightModeState(KYLIN_FLIGHT_NEED_DBUS);


	g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
	                          G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
	                          NULL,
	                          KYLIN_FLIGHT_DBUS_SERVICE,
	                          KYLIN_FLIGHT_DBUS_PATH,
	                          KYLIN_FLIGHT_DBUS_INTERFACE,
	                          g_cancellable_new (),
	                          (GAsyncReadyCallback) dbusReadyCb,
	                          &gKylinFlightInfor);

	KYLIN_COMMON_LOG(_LOGK_INFO, "kylin flight component init success!");
	return COMMON_SUCCESS;
} 

bool kylinIsSupportFlightModeDbus()
{
		GDBusProxy *proxy=NULL;
		GVariant *result;
		GError *error = NULL;
		GVariant *modValue=NULL;
		void *backValue=NULL;
		bool res =FALSE;
	
		proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
		G_DBUS_PROXY_FLAGS_NONE,NULL,
												KYLIN_FLIGHT_DBUS_SERVICE,	  
												KYLIN_FLIGHT_DBUS_PATH, 						 
												KYLIN_FLIGHT_DBUS_INTERFACE, 
											   NULL, &error);
	
		
		if(!proxy) return NULL;
		result = g_dbus_proxy_call_sync (proxy,
										"IsSupportFlightMode",
									  NULL,
									  G_DBUS_CALL_FLAGS_NONE,KYLIN_FL_DBUS_MAX_TIMEMS,
									  NULL, &error);
		if (!result) 
		{
			g_dbus_error_strip_remote_error (error);
			KYLIN_COMMON_LOG (_LOGK_ERR,"Error send log failed,%s", error->message);
			g_clear_error (&error);
			goto out;
		}
	
		//if(g_variant_is_of_type(result, G_VARIANT_TYPE_BOOLEAN))
		//{
			g_variant_get(result, "(b)",&res );
			KYLIN_COMMON_LOG(_LOGK_DEBUG, "recv back %d!",res);
		//}
	
		out:
		if (result) g_variant_unref (result);
		g_object_unref (proxy);
	
		return res;
}

bool kylinIsBindFlight()
{
	return gKylinFlightInfor.bindFlight;
}

void kylinSetBindFlight(bool bind)
{
	gKylinFlightInfor.bindFlight=bind;
}

int kylinGetFlightModeState(int isDbus)
{
	if(isDbus){
		return kylinGetFlightModeStateDbus();
	}
	return gKylinFlightInfor.flightState;
}

bool kylinIsSupportFlightMode(int isDbus)
{
	if(isDbus){
		return kylinIsSupportFlightModeDbus();
	}
	return gKylinFlightInfor.isSupport;
}


#ifdef __cplusplus
}
#endif

