#include "sr_sunrise.h"


#define PM_ADP_NAME  "luat"


//cmd: pm_exmp <mode> <fly> <time>
int PmExmp(int argc, char *argv[])
{   
	uint32_t reason = 0;
	int ret =-1;
	uint32_t sleepMode=SR_PM_SLEEP_DEEP;
	uint8_t i,fly =0;  //是否进飞行      0:不进   1:进飞行
	uint8_t ltimer = 0; //是否启用低功耗定时器
	SR_NETDEV_T *adp = SR_NetdevFind(PM_ADP_NAME);

	if(argc >=2) sleepMode = atoi(argv[1]);
	if(argc >=3) fly = atoi(argv[2]);
	if(argc >=4) ltimer = atoi(argv[3]);
	

	SR_LOG(INFO,"sleepMode: %d fly：%d",sleepMode,fly);
       
    //查询开机原因
    reason = SR_HalPmGetPoweronReason();
	SR_LOG(INFO,"PoweronReason: %d",reason);

	if(fly!=0) 
	{
	  	SR_HalNetSetCfun(adp,0,SR_CFUN_MODE_FLY);   //CFUN=0
	}
	
    
    //启动低功耗定时器,定时8秒
    if(ltimer) SR_HalPmLtimerStart(LTIMER0,7000);

	
	//休眠请求
    ret = SR_HalPmSleepReq(sleepMode);
	
	SR_LOG(INFO,"ret=%d,sleep...",ret);

	for(i=0;i<6;i++)
	{
	  osDelay(1000);
	  SR_LOG(INFO,"loop...",ret);
	}
          
       
    SR_HalPmLtimerStop(LTIMER0); //停止低功耗定时器
	SR_LOG(INFO,"Ltimer stop");
}
SR_SHELL_CMD_EXPORT(pm_exmp, PmExmp);

//查询开机原因
static int PmGetPwrReason(int argc, char *argv[])
{
	uint32_t reason;
	
	reason = SR_HalPmGetPoweronReason();	
	SR_LOG(INFO,"PoweronReason: %d",reason);
	
	return 0;
}
SR_SHELL_CMD_EXPORT(PwrReason, PmGetPwrReason);

//关机
static int PmPowerOff(int argc, char *argv[])
{
	SR_HalPmPowerOff();
	return 0;
}
SR_SHELL_CMD_EXPORT(poweroff, PmPowerOff);

//复位
static int PmReset(int argc, char *argv[])
{
	SR_HalPmReset();
	return 0;
}
SR_SHELL_CMD_EXPORT(reset, PmReset);




#if 0
#include "luat_pm.h"
#include "luat_mobile.h"


int PmExmp(int argc, char *argv[])
{   
	uint32_t reason = 0;
	int ret =-1;
	uint32_t sleepMode=SR_PM_SLEEP_DEEP;
	uint8_t i,fly =0;  //是否进飞行      0:不进   1:进飞行
	SR_NETDEV_T *adp = SR_NetdevFind(PM_ADP_NAME);

	if(argc >=2) sleepMode = atoi(argv[1]);
	if(argc >=3) fly = atoi(argv[2]);

	SR_LOG(INFO,"sleepMode: %d fly：%d",sleepMode,fly);
       
    //查询开机原因
    reason = SR_HalPmGetPoweronReason();
	SR_LOG(INFO,"PoweronReason: %d",reason);

	if(fly!=0) 
	{
		//luat_mobile_set_flymode(0, 1);  //CFUN=0
	  	SR_HalNetSetCfun(adp,0,SR_CFUN_MODE_FLY);  
	}
	
	luat_pm_request(LUAT_PM_SLEEP_MODE_NONE);
	osDelay(1000);
    
    //启动低功耗定时器,定时5秒
    SR_HalPmLtimerStart(LTIMER0,7000);

	
	//休眠请求
	if(sleepMode==1)
	{
		ret = luat_pm_request(LUAT_PM_SLEEP_MODE_IDLE);
	}
	else if(sleepMode==2)
	{
		ret = luat_pm_request(LUAT_PM_SLEEP_MODE_LIGHT);
	}
	else
	{
    	ret = SR_HalPmSleepReq(sleepMode);
	}
	SR_LOG(INFO,"ret=%d,sleep...",ret);

	for(i=0;i<6;i++)
	{
	  osDelay(1000);
	  SR_LOG(INFO,"loop...",ret);
	}
          
       
    SR_HalPmLtimerStop(LTIMER0); //停止低功耗定时器
	SR_LOG(INFO,"Ltimer stop");
}
SR_SHELL_CMD_EXPORT(pm_exmp, PmExmp);
#endif
