#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <gsl/gl_mutex.h>
#include <gsl/gl_task.h>
#include <gsl/gl_timer.h>
#include "drv_uart_def.h"
#include "al_console.h"

#include "al_uart_vehicle_ctrl.h"
#include "al_uart_vehicle.h"
#include "ap_extern.h"
#include "umf_debug.h"

extern int uartbfd;
extern CON_UART con_uart;
extern pthread_mutex_t reqMutex;

al_uint8 bStandby = TRUE;
al_uint8 reSendCount = 0;

//#define APP_AL_VEHICLE_DEBUG
#ifdef APP_AL_VEHICLE_DEBUG
#undef  AL_VEHICLE_DEBF
#define	AL_VEHICLE_DEBF(fmt, arg...)	UMFDBG(0,"[AL vehicle][%s:%d]"fmt, __FUNCTION__, __LINE__, ##arg)
#else
#define	AL_VEHICLE_DEBF(fmt, arg...)
#endif

//**************************************************************************************************************************************
//Message TV Recive vehicle Module Parm 
//**************************************************************************************************************************************
al_int32 Al_Vehicle_Get_Action(al_uint8 inCmd, void * data)
{
    if (data == NULL)
        return AL_FAILURE;
	
	switch(inCmd)
	{		
	}

    return AL_SUCCESS;
}

//**************************************************************************************************************************************
//Message TV --> vehicle Module 
//**************************************************************************************************************************************
al_int32 Al_Vehicle_Set_Action(al_uint8 inCmd, al_uint8 * data, al_uint8 size)
{
    al_uint8 *packet = NULL;
	
	packet = malloc(size);
	if (packet == NULL)
    {
    	AL_VEHICLE_DEBF("%s  malloc fail!\n",__FUNCTION__);
        return VEHICLE_ERR_FAILURE;
    }
	
	memset(packet,0x0,size);
	memcpy(packet,data,size);
	
	Vehicle_sendCmdToModule(packet, size);
	free(packet);
	packet = NULL;

    return AL_SUCCESS;
}

void Al_Vehicle_CheckResend()
{
	al_uint16 tmp = 0;

	while(tmp)
	{

		if(tmp)
			GL_TaskSleep(100);
	}

	pthread_mutex_lock(&reqMutex);
	
	if(reSendCount)
		reSendCount--;
	else
	{
		reSendCount = 0;
	}
	pthread_mutex_unlock(&reqMutex);
}

al_int Al_Vehicle_Listener(void *argv)
{
    pCON_DEV pcon = (pCON_DEV) argv;
    al_int8 rec[512] = { 0 };
    al_uint readLen = 0;
    bool bLeave = false, bReset = false;
    al_uint8 buf[512] = { 0 };
    Vehicle_MessageBody_t stEvt = { 0 };


    uartbfd = open("/dev/uartb", O_RDWR);

    if (uartbfd <= 0)
    {
        //condbg2(pcon->dbg,"Open Console Fail \n");
        return AL_FAILURE;
    }

    pcon->n = 0;
    pcon->pid = getpid();

    while (pcon->bActive)
    {
        while (1)
        {
            con_uart.UART_UARTB_Listen_Status = TRUE;
            memset(buf, 0x0, sizeof(buf));
            GL_TaskSleep(300);
            readLen = read(uartbfd, buf, sizeof(buf));

            if (readLen > 0 || con_uart.uartB_Stop)
            {
                con_uart.UART_UARTB_Listen_Status = FALSE;
                break;
            }
        }

        if (!pcon->bActive)
        {
            break;
        }

        if (readLen <= 0)
        {
            AL_VEHICLE_DEBF("*** UARTB_data->len = 0 *** \n");
        }
		else
		{
		    memcpy(&rec[pcon->n], buf, readLen);
		    pcon->n += readLen;
		}

		AL_VEHICLE_DEBF("Head =[0x%x], pcon->n =[0x%x]\n",rec[0], pcon->n);
		memset(&stEvt,0x0,sizeof(Vehicle_MessageBody_t));
        switch (rec[0])
        {
          case CMD_VEHICLE_HEADER:
		  	//AL_VEHICLE_DEBF("pcon->n =[0x%x], CheckSum:%x,%x\n",
			//	pcon->n,(al_uint8)rec[pcon->n-1],AL_Vehicle_Get_UARTCmd_CheckSum((al_uint8 *)rec,13));
			if (((al_uint8)rec[pcon->n - 2] == 0xfe) && ((al_uint8)rec[pcon->n - 1] == 0xff) && ((al_uint8)rec[13] == AL_Vehicle_Get_UARTCmd_CheckSum((al_uint8 *)rec,13)))
			{
			  AL_VEHICLE_DEBF("CheckSum (0x%x) is right !\n",(al_uint8)rec[13]);
			  stEvt.dCmdSize = 16;//not include head+cmd+sum
			  //stEvt.dMessageCmd = (al_uint8)rec[2];
			  stEvt.pdata = (al_uint8*)&rec[1];			
			}
			bReset = true;
			break;
			
          default:
              bReset = true;
              break;
        }

        Vehicle_SendEvtToAp(stEvt);

        if (bReset)
        {
            bReset = false;
            memset(&rec, 0, pcon->n);
            pcon->n = 0;
        }

        if (bLeave)
        {
            pcon->bActive = FALSE; // for UART B to leave its while loop
            con_uart.uartB_Stop = TRUE;
            break;
        }
    }

    pcon->pid = 0;
    close(uartbfd);

    return AL_SUCCESS;

}

