#include "includes.h"
#include "bsp.h"

#define VIR_WAIT_FOREVER (-1U)

LSAPI_OSI_Pipe_t *at_rx_pipe = NULL;
LSAPI_OSI_Pipe_t *at_tx_pipe = NULL;


void vir_at_process(char *buf,unsigned int len)
{
	
	if(sutApp.gtTMode==1)MSG_INFO(1,"vir_at--->%s",buf);
	if(msgCmp(buf, "SV"))	{
		strcpy(sutApp.modemVer, buf);
			sutApp.modemVer[strlen(buf)-2]=0;
		}
	
	pocCmdHandler(buf+2,len-2);// bnd�лس����� +2
	//cmdsProcess(buf+2,len-2);
}
//模块应答
static void prvVirtAtRespCallback(void *param, unsigned event)
{
    LSAPI_OSI_Pipe_t *pipe = (LSAPI_OSI_Pipe_t *)param;
    char buf[256];
    
    for (;;)
    {
        int bytes = LSAPI_OSI_PipeRead(pipe, buf, sizeof(buf)-1);
        if (bytes <= 0)
            break;

        buf[bytes] = '\0';
       // MSG_INFO(1,"VAT1:%d,%s",bytes,buf);
        vir_at_process(buf,bytes);
    }
   	//MSG_INFO(1,"prvVirtAtRespCallback");
}

void makeupsample(void *param){
    at_rx_pipe = LSAPI_OSI_PipeCreate(1024);
    at_tx_pipe = LSAPI_OSI_PipeCreate(1024);
    LSAPI_OSI_PipeSetReaderCallback(at_tx_pipe, LSAPI_PIPE_EVENT_RX_ARRIVED,
                             prvVirtAtRespCallback, at_tx_pipe);

    LSAPI_Device_AtVirtConfig_t cfg = {
        .name = LSAPI_MAKE_TAG('V', 'A', 'T', '1'),
        .rx_pipe = at_rx_pipe,
        .tx_pipe = at_tx_pipe,
    };
    LSAPI_Device_t *device = LSAPI_Device_AtVirtCreate(&cfg);
    LSAPI_Device_AtDispatch_t *dispatch = LSAPI_Device_AtDispatchCreate(device);
    LSAPI_Device_AtSetDispatch(device, dispatch);
    LSAPI_Device_Open(device);
    LSAPI_OSI_ThreadExit();
}

void CreateSerialAtThead()
{

	if(NULL==LSAPI_OSI_ThreadCreate("virat", makeupsample, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024, 4))
			MSG_INFO(1,"virat thread create err");
		//下面的延时是一定要的,否则VAT1的回调无法进�?
		//情况�?如果不加下面的延时,只要执行bspStartNotice往串口输出数据,VAT1就无法回调了
		LSAPI_OSI_ThreadSleep(500);	

}

/*
innerInfo
发送数据到内部(module)API
*/

void innerInfo(unsigned char  *info, unsigned int len){
	if(len>0){
		if(0>LSAPI_OSI_PipeWriteAll(at_rx_pipe, info, len, VIR_WAIT_FOREVER))
			MSG_INFO(1,"LSAPI_OSI_PipeWriteAll write failed");
	}
}

void msgToModem(char *p)
{
	int len;
	len=strlen(p);
	innerInfo(p,len);
}