/*****************************************************************************
 函 数 名 : dual_modem_wakeup_init
 功能描述 : 初始化睡眠投票,初始化定时器,注册icc通信
 输入参数 : 无
 输出参数 : 0:成功,-1:失败
 返 回 值 : int
*****************************************************************************/
int dual_modem_wakeup_init(DRV_DUAL_MODEM_STR DualModemNv)
{
	struct softtimer_list *T1_timer =&g_dual_modem_ctrl.hold_wake_timer;
	struct softtimer_list *T2_timer =&g_dual_modem_ctrl.sleep_timer;
	T1_timer->func = modem_awaketimer_int;
	T1_timer->para = 0;	
	T1_timer->timeout = DualModemNv.AwakeTmer;
	T1_timer->wake_type = SOFTTIMER_NOWAKE;
	
	T2_timer->func = modem_sleeptimer_int;
	T2_timer->para = 0;	
	T2_timer->timeout= DualModemNv.DoSleepTimer;
	T2_timer->wake_type = SOFTTIMER_NOWAKE;

	wake_lock_init(&g_dual_modem_ctrl.wakelock ,PWRCTRL_SLEEP_UART0 ,"PWRCTRL_SLEEP_UART0");

 	g_dual_modem_ctrl.wakeup_3rdmodem_flag = DO_WAKEUP_3RD;
	if(OK !=bsp_softtimer_create(T1_timer))
	{
		dm_print_err("T1_timer create fail!\n");
		goto T1timerfail;
	}
	if(OK !=bsp_softtimer_create(T2_timer))
	{
		dm_print_err("T2_timer create fail!\n");
		goto T2timerfail;
	}
	return OK;
	
T2timerfail:
	bsp_softtimer_free(T1_timer);
T1timerfail:
	return ERROR;	
}
int bsp_om_start_timer(u32 timer_len,void *func,u32 para,struct softtimer_list *timer)
{
    s32 ret = BSP_OK;

    timer->func = func;
    timer->para = para;
    timer->timeout = timer_len*1000;
    timer->wake_type = SOFTTIMER_NOWAKE;

    if(timer->init_flags != TIMER_INIT_FLAG)
    {
        ret = bsp_softtimer_create(timer);


        bsp_softtimer_add(timer);

    }
    else
    {
        ret =bsp_softtimer_modify(timer,timer_len*1000);

        bsp_softtimer_add(timer);
    }
    return ret;
}
/*测试非唤醒源软timer申请和加入链表,LOOP模式,执行一段时间后在回调函数外delete*/
s32 st_softtimer_test_case14(void)
{
	s32 ret=0;
	
	my_softtimer.func = for_softtimer_test_8;
	my_softtimer.para = 123;
	my_softtimer.timeout = 1;
	my_softtimer.wake_type=SOFTTIMER_NOWAKE;
	
	if (bsp_softtimer_create(&my_softtimer))
	{
		bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"create error\n");
		return ERROR;
	}
	bsp_softtimer_add(&my_softtimer);
	delay(200);
	ret = bsp_softtimer_delete_sync(&my_softtimer);
	if(OK!=ret)
	{
		bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"bsp_softtimer_delete_sync failed\n");
		return ERROR;
	}
	ret = bsp_softtimer_free(&my_softtimer);
	 if (ret !=OK)
	 {
	 	bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"st_softtimer_test_case16  free error\n");
		return ERROR;
	 }
	 
	 return OK;
}
/*测试非唤醒源软timer申请和加入链表,在回调函数外删除软timer
   超时时间较长*/
s32 st_softtimer_test_case13(void)
{
	s32 ret= 0;
	
	my_softtimer.func = for_softtimer_test_1;
	my_softtimer.para = 123;
	my_softtimer.timeout = 100;
	my_softtimer.wake_type=SOFTTIMER_NOWAKE;
	osl_sem_init(0,&sem1);
	
	 if (bsp_softtimer_create(&my_softtimer))
	   {
		bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"create error \n");
	    }
	 bsp_softtimer_add(&my_softtimer);
	ret = bsp_softtimer_delete(&my_softtimer);
	if (ret ==OK)
	{
	   ret = bsp_softtimer_free(&my_softtimer);
	   if (ret !=OK)
	   {
	   	bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"softtimer_test_6  free error\n");
		return ERROR;
	   }  
	   
	}
	else if(NOT_ACTIVE == ret)
	{
		osl_sem_down(&sem1);
		osl_sema_delete(&sem1);
	}
	return ret;	
}
/*测试非唤醒源软timer申请和加入链表*/
s32 st_softtimer_test_case11(void)
{
	
	my_softtimer.func = for_softtimer_test_1;	
	my_softtimer.wake_type=SOFTTIMER_NOWAKE;
	my_softtimer.para = 1;
	my_softtimer.timeout = 100;
	osl_sem_init(0,&sem1);
	osl_sem_init(0,&sem5);
	osl_sem_init(0,&sem6);
	if (bsp_softtimer_create(&my_softtimer))
	{
		bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"create error \n");
		return ERROR;
	}
	bsp_softtimer_add(&my_softtimer);
	my_sec_softtimer.func = for_softtimer_test_5;
	my_sec_softtimer.para = 2;
	my_sec_softtimer.timeout = 200;
	if (bsp_softtimer_create(&my_sec_softtimer))
	{
		bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"create error \n");
		return ERROR;
	}
	bsp_softtimer_add(&my_sec_softtimer);
	my_thir_softtimer.func = for_softtimer_test_6;
	my_thir_softtimer.para = 3;
	my_thir_softtimer.timeout = 300;
	if (bsp_softtimer_create(&my_thir_softtimer))
	{
		bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"create error \n");
		return ERROR;
	}
	bsp_softtimer_add(&my_thir_softtimer);
	osl_sem_down(&sem1);
	osl_sem_down(&sem5);
	osl_sem_down(&sem6);
	osl_sema_delete(&sem1);
	osl_sema_delete(&sem5);
	osl_sema_delete(&sem6);
	return OK;
}
/******************************************************************************
*  Function:  drv_shut_down
*  Description: start the power off process.
*  Input:
*         eReason : shutdown reason.
*  Output:
*         None
*  Return:
*         None
*  Note  : 底层调用关机接口,启用定时器,上报事件给应用。
*          超时时间内应用不关机,由底层强制关机。
********************************************************************************/
void drv_shut_down( DRV_SHUTDOWN_REASON_ENUM enReason )
{
#ifndef HI_ONOFF_PHONE
	power_off_ctrl.off_timer.func = (softtimer_func)power_off_timeout_isr;
    if( DRV_SHUTDOWN_CHARGE_REMOVE == enReason )
    {
        if( DRV_START_MODE_NORMAL == bsp_start_mode_get() )
        {
            /* In normal mode, don't handle the charge remove message */
            return ;
        }
    }

    if( DRV_SHUTDOWN_BUTT == enReason )
    {
        return ; /* the shutdown been handled, ignore this call */
    }

    /* save the reason and drv_power_off need it */
    power_off_ctrl.reason = enReason;

	power_off_ctrl.off_timer.para = (u32)0;
	power_off_ctrl.off_timer.timeout = POWER_OFF_MONOTER_TIMEROUT;
	power_off_ctrl.off_timer.wake_type = SOFTTIMER_WAKE;

    if (bsp_softtimer_create(&power_off_ctrl.off_timer))
    {
        pr_dbg("create softtimer failed \n");
    }

    bsp_softtimer_add(&power_off_ctrl.off_timer);

    /* 针对由底层上报关机事件,由应用执行关机的场景 */
    /* TO DO */

    power_off_log_save();
#else
    if (DRV_SHUTDOWN_RESET == enReason)
    {
        pr_dbg("drv_shut_down is called, modem reset...");

        bsp_modem_reset();
    }
#endif
    /* Notify the monitor task */
}
void st_softtimer_test_case16(void)
{
	u32 i=0;
	for(i=0;i<40;i++)/*lint !e52 */
	{
		myown_timer[i].func = for_softtimer_test_7;
		myown_timer[i].wake_type=SOFTTIMER_WAKE;
		myown_timer[i].para = i;
		myown_timer[i].timeout = 1000+i*1000;
		if (bsp_softtimer_create(&myown_timer[i]))
		{
			bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"create error i =%d\n",i);
			return ;
		}
		bsp_softtimer_add(&myown_timer[i]);
	}	
}
/*****************************************************************************
 函 数 名  : power_on_timer_init
 功能描述  : 创建开机timer,用于开机后一段时间检测系统是否正常工作
 输入参数  :
 输出参数  : 无
 返 回 值  :
 调用函数  :
 被调函数  :
*****************************************************************************/
void power_on_timer_init( void )
{
	s32 ret = 0;

	power_item_info.on_timer.func = (softtimer_func)power_on_timeout_isr;

	power_item_info.on_timer.para = (u32)0;
	power_item_info.on_timer.timeout = POWER_ON_MONITOR_TIMEROUT;
	power_item_info.on_timer.wake_type = SOFTTIMER_WAKE;

	ret = bsp_softtimer_create(&power_item_info.on_timer);
    if (ret)
    {
        pr_dbg("create softtimer failed \n");
    }

    bsp_softtimer_add(&power_item_info.on_timer);
}
/*测试唤醒源软timer申请和加入链表重复10次,每次都在回调函数修改超时时间*/
s32 st_softtimer_test_case03(void)
{
	
	my_softtimer.func = for_softtimer_test_3;	
	my_softtimer.para = 2;
	my_softtimer.timeout = 10;
	my_softtimer.wake_type=SOFTTIMER_WAKE;
	osl_sem_init(0,&sem3);
	if (bsp_softtimer_create(&my_softtimer))
	{
		bsp_trace(BSP_LOG_LEVEL_ERROR,BSP_MODU_SOFTTIMER,"create error\n");
		return ERROR;
	}
	bsp_softtimer_add(&my_softtimer);
	osl_sem_down(&sem3);
	osl_sema_delete(&sem3);
	return OK;
}
/*******************************************************************************
 * FUNC NAME:
 * led_softtimer_create() 
 * Function     : 创建软timer
 * Arguments
 *      input   : @timer - timer的定时时间
 *                @wake_type - timer对CPU的唤醒类型
 *      output  : null
 *
 * Return       : LED_OK - 成功;else - 失败
 * Decription   : null
 ************************************************************************/
int led_softtimer_create(unsigned int time, enum wakeup wake_type)
{
	led_softtimer.func = (softtimer_func)led_state_check_callback;
	led_softtimer.para = 0;                              
	led_softtimer.timeout = time;                        /* 定时长度,单位ms */
	led_softtimer.wake_type = wake_type;


	if (bsp_softtimer_create(&led_softtimer))
	{
        LED_TRACE(LED_DEBUG_LEVEL(ERROR),"[%s]ERROR: softtimer create failed\n", __FUNCTION__);
		return LED_ERROR;
	}
    
	bsp_softtimer_add(&led_softtimer);

    LED_TRACE(LED_DEBUG_LEVEL(INFO), "LED create softtimer OK\n");
    
    return LED_OK;
}
s32 edma2_stress_test_start(s32 task_priority,s32 test_rate)
{
    s32 ret = 0;
    u32 index = 0;

    s32 chann_id = 0;/*bsp_edma_channel_init(EDMA_MEMORY, 0, 0, 0);*/

    for (index=0;index<EDMA_HANDLE_NUM;index++)
    {
        g_edma2_busstress_info.stDMATaskHandle[index].ulChIdx = chann_id;
    }
    logMsg("edma2_stress: use axi 0x%x \n", MEM_FOR_EDMAC2_BUSSTRESS,0,0,0,0,0);
    /*
    g_edma2_busstress_info.stDMATaskHandle[0] = {0, 0,  EDMA_TEST_LEN,  chann_id};
    g_edma2_busstress_info.stDMATaskHandle[1] = {0, EDMA2_HANDLE_1_AXI_MEM_64_DST,   EDMA_TEST_LEN,  chann_id};
    g_edma2_busstress_info.stDMATaskHandle[2] = {EDMA2_HANDLE_2_AXI_MEM_64_SRC,  EDMA2_HANDLE_2_AXI_MEM_64_DST, EDMA_TEST_LEN,  chann_id};
    g_edma2_busstress_info.stDMATaskHandle[3] = {EDMA2_HANDLE_3_AXI_MEM_64_SRC,  0,    EDMA_TEST_LEN,  chann_id};
    */
    set_test_switch_stat(EDMAC, TEST_RUN);

    /*创建控制发送速率的信号量*/
    /*sema_init(&(g_edma2_busstress_info.edma_send_sem) , SEM_EMPTY);*/
    g_edma2_busstress_info.edma_send_sem = semBCreate(SEM_Q_FIFO, SEM_EMPTY);

    if (g_edma2_busstress_info.edma_send_sem == NULL)
    {
        printf("semBCreate gmac_send_sem fail.\n");
        return ERROR;
    }
    g_edma2_busstress_info.edma_softtimer_list.func = ( softtimer_func )edma2_timer_event;
    g_edma2_busstress_info.edma_softtimer_list.para = 0;
    g_edma2_busstress_info.edma_softtimer_list.timeout = test_rate;
    g_edma2_busstress_info.edma_softtimer_list.wake_type =SOFTTIMER_NOWAKE;

    /*创建控制发送速率 的软timer*/
    if( bsp_softtimer_create(&g_edma2_busstress_info.edma_softtimer_list))
    {
        printf("SOFTTIMER_CREATE_TIMER fail.\n");
        /*sema_delete(g_edma2_busstress_info.edma_send_sem);*/
        semDelete(g_edma2_busstress_info.edma_send_sem);
        return ERROR;
    }

    g_edma2_busstress_info.stDMATaskHandle[0].u32SrcAddr = (u32)cacheDmaMalloc(EDMA_TEST_LEN);
    g_edma2_busstress_info.stDMATaskHandle[0].u32DstAddr = (u32)cacheDmaMalloc(EDMA_TEST_LEN);
    g_edma2_busstress_info.stDMATaskHandle[1].u32SrcAddr = (u32)cacheDmaMalloc(EDMA_TEST_LEN);
    g_edma2_busstress_info.stDMATaskHandle[3].u32DstAddr = (u32)cacheDmaMalloc(EDMA_TEST_LEN);
    /*创建发包测试任务,收包驱动会自行做处理*/
    g_edma2_busstress_info.edma_stress_test_task_id \
            = taskSpawn ("edmaBusTask", task_priority, 0, 20000, (FUNCPTR)edma2_stress_test_routine, 0,
                                        0, 0, 0,0, 0, 0, 0, 0, 0);
    if(g_edma2_busstress_info.edma_stress_test_task_id == ERROR)
    {
        printf("taskSpawn edma2BusTask fail.\n");
        /*sema_delete(g_edma2_busstress_info.edma_send_sem);*/
        semDelete(g_edma2_busstress_info.edma_send_sem);
        ret = bsp_softtimer_free(&g_edma2_busstress_info.edma_softtimer_list);
    }
    else
    {
        bsp_softtimer_add( &g_edma2_busstress_info.edma_softtimer_list );
    }
    return g_edma2_busstress_info.edma_stress_test_task_id;
}