/************************************************************************************
 * @Copyright (c)    :(C)2017, Qindao ieslab Co., Ltd
 * @FileName   	     :pic24_nb_uart_driver.c
 * @Author           :QDIES PLATFORM TEAM
 * @Version          :V1.0
 * @Date             :2017-03-01
 * @Description      :
 ************************************************************************************/
#include "sys_config.h"
#ifdef NB_MODULE_MN316
#ifdef	__cplusplus
extern "C"
{
#endif
#include "nb_uart_driver_mn316.h"
#include "nb_driver_mn316.h"
#include <time.h>
#include "UWater_Rtcc_driver.h"
#include "UWater_DebugPrint.h"

/********************************变量定义************************************/
UART_COMM Uart_Comm;
u8 g_s_rev_state; //接收状态机的状态变量
u8 g_s_rev_state_coap; //coap接收状态机的状态变量
u8 g_s_rev_state_onenet; //coap接收状态机的状态变量
u8 g_s_rev_state_com; //接收状态机的状态变量
u8 udp_check_rev_data_flag = FALSE;

//信号强度和ICCID串号
u8 g_csq_value = 0x32;
u8 g_band_val = 0;
u8 g_ecl_snr_value[11] = {0};
u8 g_date_time_num = 0;
u8 s_g_buf[10] = {0};
u8 s_g_temp1 = 0;

char buf_imei[16];//IMEI号码为15 BIT阿拉伯数字 最后一字节为结束符 “\0”
char buf_imsi[16];//IMSI号码为15 BIT阿拉伯数字 最后一字节为结束符 “\0”
char buf_iccid[21];//ICCID为20位 数字或者大写字符 

u8  s_g_apn_value[APN_SIZE] = {0};
u32 s_g_cell_id_value = 0;
struct tm tm_GMT_nb_uart_driver= {0,0,0,0,0,0,0,0,0}; //格林尼时间
u32 time_temp_nb_uart_driver;
static char g_tmp_byte[11] = {0};
int s_g_earfcn_value = 0;//
u32 s_g_earfcn_offset_value = 0;
int s_g_pci_value = 0;
char s_g_cell_id[CELL_ID_SIZE];
int s_g_rsrp_value = 0;//带符号可为负值
int s_g_rsrq_value = 0;//带符号可为负值
int s_g_rssi_value = 0;//带符号可为负值
int s_g_snr_value = 0;
u16 s_g_band_value = 0;
char s_g_tac[10];
int s_g_ecl_value = 0;
s16 s_g_tx_pwr = 0;

u32 nb_uart_time_num;
u32 nb_uart_time_old_num;
volatile u8 data = 0;
//接收推送的数据长度
u8 g_s_socket_rev_len=0;
u8 g_s_tcp_socket='0';
u8 g_s_udp_socket='1';
u8 g_s_rev_socket_len_state=0;
u8 udp_rev_data_len=0;
/*****************************内部功能状态机***********************************/
static void AT_OK_AND_ERROR_MACHINE(u8 data);
static void AT_CGACT_HOW_MACHINE(u8 data);
static void AT_CEREG_MACHINE(u8 data);
static void AT_CIMI_MACHINE(u8 data);
static void AT_NBIOT_REV_MACHINE(u8 data);
static void AT_CSQ_MACHINE(u8 data);
static void AT_IMEI_MACHINE(u8 data);
static void AT_GET_CCLK_MACHINE(u8 data);
static void AT_ICCID_MACHINE(u8 data);
static void AT_CPIN_MACHINE(u8 data);
static void AT_MIPOPEN_MACHINE(u8 data);
static void AT_MIPSENDCOUNT_MACHINE(u8 data);
static void AT_MIPSEND_MACHINE(u8 data);
static void AT_MIPCLOSE_MACHINE(u8 data);
static void AT_VERCTRL_CK_MACHINE(u8 data);
static void AT_TUEINFO_MACHINE(u8 data);
static void AT_CCED_MACHINE(u8 data);
static void AT_CGATT_HOW_MACHINE(u8 data);
static void AT_NSONMI_MACHINE(u8 data);
static void AT_NBIOT_READ_MACHINE(u8 data);
static void AT_MCWOPEN_MACHINE(u8 data);
static void AT_MCWSEND_ACK_MACHINE(u8 data);
static void AT_NBIOT_REV_MACHINE_MN316_CTWing(u8 data);
static void AT_QIOPEN_MACHINE(u8 data);
static void AT_NUWSTATS_MACHINE(u8 data);
static void AT_VERCTRL_CK_MACHINE(u8 data);
static void AT_NV_CK_MACHINE(u8 data);
static void AT_NRB_CK_MACHINE(u8 data);
static void AT_MIPLCREATEEX(u8 data);
static void AT_MIPLOPEN_MACHINE(u8 data);
static void AT_WAIT_MIPLOBSERVE(u8 data);
static void AT_NMGS_ACKID_MACHINE(u8 data);
/***************************************************************************
 * @fun_name     static void UART_Receive_IT(NBIOT_UART_HandleTypeDef *huart)
 * @brief        串口接收中断处理程序
 ***************************************************************************/
void NBUartReceiveIT(void)
{
	data = LPUart_ReceiveData(M0P_LPUART1);

	AT_NSONMI_MACHINE(data);
	AT_NBIOT_REV_MACHINE(data);//tcp/udp 接收数据一直查询
	//AT_NBIOT_REV_MACHINE_MN316_CTWing(data);
	AT_NMGS_ACKID_MACHINE(data);//ACK应答检查
    switch (Uart_Comm.send_style)
    {
        case cst_AT_No_AT:
		case cst_AT_No_AT_CMEE:
        case cst_AT_No_AT_CFUN:
        case cst_AT_No_AT_CFUN0:
        case cst_AT_No_AT_CFUN1:
		case cst_AT_No_AT_MCWCREATE:
		case cst_AT_No_AT_MCWSEND:
		case cst_AT_No_AT_NMGS: 
		case cst_AT_No_AT_NVSET:
		case cst_AT_No_AT_MIPLADDOBJ:
		case cst_AT_No_AT_MIPLDISCOVERRSP:
		case cst_AT_No_AT_MIPLUPDATE:
        {
            AT_OK_AND_ERROR_MACHINE(data); 
        }
        break;

        case cst_AT_No_AT_CIMI:
        {
            AT_CIMI_MACHINE(data);
        }
        break;
		case cst_AT_No_AT_CGATT_how:
        {
            AT_CGATT_HOW_MACHINE(data);
        }
        break;
        case cst_AT_No_AT_CGACT_how:
        {
            AT_CGACT_HOW_MACHINE(data);
        }
        break;

        case cst_AT_No_AT_CEREG:
        {
            AT_CEREG_MACHINE(data);
        }
        break;

        case cst_AT_No_AT_CSQ:
        {
            AT_CSQ_MACHINE(data); 
        }
        break;

        case cst_AT_No_AT_IMEI:
        {
            AT_IMEI_MACHINE(data);
        }
        break;

        case cst_AT_No_AT_CCLK:
        {
            AT_GET_CCLK_MACHINE(data);
        }
        break;

        case cst_AT_No_AT_ICCID:
        {
            AT_ICCID_MACHINE(data);
        }
        break;	
		
		case cst_AT_No_AT_CPIN:                    
        {
            AT_CPIN_MACHINE(data); 
        }
        break;
		case cst_AT_No_AT_MIPOPEN:                    
        {
            AT_MIPOPEN_MACHINE(data); 
        }
        break;
		case cst_AT_No_AT_MIPSENDCOUNT:
		{
			AT_MIPSENDCOUNT_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_TCPSEND:
		{
			AT_MIPSEND_MACHINE(data); 
		}
		break;
		case cst_AT_No_AT_MIPCLOSE:
		{
			AT_MIPCLOSE_MACHINE(data); 
		}
		break;
		case cst_AT_No_VERCTRL_CK:
		{
			AT_VERCTRL_CK_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_TUEINFO:
		{
			AT_TUEINFO_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_CCED:
		{
			AT_CCED_MACHINE(data);
		}
		break; 
		case cst_AT_No_AT_READ_DATA:
		{
			AT_NBIOT_READ_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_MCWOPEN:
		{
			AT_MCWOPEN_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_MCWSEND_ACKID:
		{
			AT_MCWSEND_ACK_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_QIOPEN:
		{
			AT_QIOPEN_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_NUESTATS:
		{
			AT_NUWSTATS_MACHINE(data);
		}
		break;

		case cst_AT_No_AT_NVGET:
		{
			AT_NV_CK_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_NRB: 
		{
			AT_NRB_CK_MACHINE(data);
		}
		break;
		
		case cst_AT_No_AT_MIPLCREATEEX:
		{
			AT_MIPLCREATEEX(data);
		}
		break;
		case cst_AT_No_AT_MIPLOPEN: 
		{
			AT_MIPLOPEN_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_MIPLOBSERVE: 
		{
			AT_WAIT_MIPLOBSERVE(data);
		}
		break;
	
        default:
            break;
    }
	s_g_buf[0] = data;
    PrintNbData(s_g_buf,1);//打印源码
//	AT_CHECK_ERROR_NUM_MACHINE(data);
}
/***************************************************************************
 * @fun_name     void NbiotUartSend(u8 *buf, u16 len, u8 style)
 * @brief        发送数据
 * @param[in] 	 buf：发送数据的地址  len：发送长度
				 style：发送方式（其他：AT指令、短信、NBIOT数据）
 ***************************************************************************/
void NbiotUartSend(u8 *buf, u16 len, u8 style)
{
    static u8 send_msg_bc25;
    if (len > 0)
    {
        UartSend(NB_UART_COM, len,buf, &send_msg_bc25);
		PrintNbData(buf,len);
        //状态初始化
        g_s_rev_state = S0;
		g_s_rev_state_com = 0;
		g_s_rev_state_coap = 0;
		g_s_rev_state_onenet = 0;
        Uart_Comm.rev_ack_flag = UART_ING;
        Uart_Comm.send_style = style;
    }
}

/***************************************************************************
 * @fun_name     s8 NbiotCheckAtAck()
 * @brief        查询串口返回数据是否正确
 * @param[in] 	 None
 * @param[out]   None
 * @retval     	 UART_ING:接收中 UART_OK：正确帧 UART_ERROR：错误帧
 * @other        None
 ***************************************************************************/
s8 NbiotCheckAtAck()
{
    return Uart_Comm.rev_ack_flag;
}

void NbiotClearAtAck (void)
{
  Uart_Comm.rev_ack_flag = 0;	
}
/***************************************************************************
 * @fun_name    void NbiotRcv(u16 *len , u8 **data)
 * @brief       获取NBIOT数据
 * @param[out] 	data: 数据存放地址  len:接收长度存放地址
 ***************************************************************************/
void NbiotRcvTransOne(u16 *len, u8 **data)
{
    u16 i;
    u8 temp_data;
    for (i = 0; i < Uart_Comm.rev_len / 2; i++)
    {
        if (Uart_Comm.rev_buf[i * 2] > 0x5A)//大于‘Z’，则为小写字母，‘a’的ASCII码值为97，则应减87
        {
            temp_data = (Uart_Comm.rev_buf[i * 2] - 0x57) << 4;
        }
        else if (Uart_Comm.rev_buf[i * 2] > 0x39)
        {
            temp_data = (Uart_Comm.rev_buf[i * 2] - 0x37) << 4;
        }
        else
        {
            temp_data = (Uart_Comm.rev_buf[i * 2] - 0x30) << 4;
        }

        if (Uart_Comm.rev_buf[i * 2 + 1] > 0x5A)
        {
            temp_data |= (Uart_Comm.rev_buf[i * 2 + 1] - 0x57);
        }
        else if (Uart_Comm.rev_buf[i * 2 + 1] > 0x39)
        {
            temp_data |= (Uart_Comm.rev_buf[i * 2 + 1]) - 0x37;
        }
        else
        {
            temp_data |= (Uart_Comm.rev_buf[i * 2 + 1]) - 0x30;
        }
        Uart_Comm.rev_buf_handled[i] = temp_data;
    }
    *data = Uart_Comm.rev_buf_handled;
    *len = Uart_Comm.rev_len / 2;
}
void NbiotRcvUdp(u16 *len, u8 **data)
{
	u16 i;
    for (i = 0; i < Uart_Comm.rev_len; i++)
    {
        Uart_Comm.rev_buf_handled[i] = Uart_Comm.rev_buf[i];
    }
    *data = Uart_Comm.rev_buf_handled;
    *len = Uart_Comm.rev_len;
}
void NbiotRcv(u16 *len, u8 **data)
{
	if((GetRcvDataType() == NB_RCV_UDP1_DATA)||(GetRcvDataType() == NB_RCV_UDP2_DATA))
	{
		NbiotRcvUdp(len, data);
	}
	else
	{
		NbiotRcvTransOne(len, data);
	}
}
/***************************************************************************
 * @fun_name    u8 NbiotRcvFinish()
 * @brief       NBIOT是否接收完数据
 ***************************************************************************/
u8 NbiotRcvFinish()
{
    return Uart_Comm.nbiot_rev_flag;
}
/***************************************************************************
 * @fun_name    void NbiotClearMsg()
 * @brief       清NBIOT接收标志位
 * @param[out] 	data: 数据存放地址  len:接收长度存放地址
 ***************************************************************************/
void NbiotClearMsg()
{
    Uart_Comm.nbiot_rev_flag = 0;
    Uart_Comm.send_style = 0;//cst_AT_No_AT_NBIOT_REV;
    Uart_Comm.rev_len = 0;
    g_s_rev_state = S0;
}
//返回OK指令(解析处理流程: 前导为0x4F('O'), 向后检测(K(OK)或者R(ERROR)),再检测结束符(0x0D,0x0A))
static void AT_OK_AND_ERROR_MACHINE(u8 data)
{
    static u8 s_state_flag;
    switch (g_s_rev_state)
    {
        case S0:
        {
            if (data == 'O')
            {
                g_s_rev_state = S1;
            }
        }
        break;

        case S1:
        {
            if (data == 'K')
            {
                s_state_flag = 1;
                g_s_rev_state = S2;
            }
            else if (data == 'R')
            {
                s_state_flag = 0;
                g_s_rev_state = S2;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S2:
        {
            if (data == '\r')
            {
                g_s_rev_state = S3;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S3:
        {
            if (data == '\n')
            {
                if (s_state_flag)
                {
                    Uart_Comm.rev_ack_flag = UART_OK;
                }
                else
                {
                    Uart_Comm.rev_ack_flag = UART_ERROR;
                }
            }
            g_s_rev_state = S0;
        }
        break;

        default:
        {
            g_s_rev_state = S0;
        }
        break;
    }
}
/**
AT+CEREG?
+CEREG: 0,1
*/
//#define AT_CHECK_LEN_CEREG   11
//static const u8 scAT_CEREG[AT_CHECK_LEN_CEREG]="+CEREG: 0,1";
#define AT_CHECK_LEN_CEREG   10
static const u8 scAT_CEREG[AT_CHECK_LEN_CEREG]="+CEREG:0,1";
static void AT_CEREG_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_CEREG-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == '1')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_CEREG)
			{
				g_s_rev_state_com = ((scAT_CEREG[g_s_rev_state_com] == data)? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
AT+CGACT?
+CGACT: 1,1
*/
//#define AT_CHECK_LEN_CGACT   11
//static const u8 scAT_CGACT[AT_CHECK_LEN_CGACT]="+CGACT: 1,1";
#define AT_CHECK_LEN_CGACT   10
static const u8 scAT_CGACT[AT_CHECK_LEN_CGACT]="+CGACT:1,1";
static void AT_CGACT_HOW_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_CGACT-3):
		 {
			g_s_rev_state_com = ((('1' == data)||('5' == data))? (g_s_rev_state_com+1) : 0); 
			break;
		 } 
		 case(AT_CHECK_LEN_CGACT-2):
		 {
			g_s_rev_state_com = (',' == data? (g_s_rev_state_com+1) : 0); 
			break;
		 }
		 case(AT_CHECK_LEN_CGACT-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == '1')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_CGACT)
			{
				g_s_rev_state_com = (scAT_CGACT[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}

/**
AT+CGACT?
+CGACT: 1,1
*/
//#define AT_CHECK_LEN_CGACT   11
//static const u8 scAT_CGACT[AT_CHECK_LEN_CGACT]="+CGACT: 1,1";
#define AT_CHECK_LEN_CGATT   8
static const u8 scAT_CGATT[AT_CHECK_LEN_CGATT]="+CGATT:1";
static void AT_CGATT_HOW_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_CGATT-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == '1')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_CGATT)
			{
				g_s_rev_state_com = (scAT_CGATT[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}

//查询IMSI 正确格式为 CIMI号+OK 先把数据取出来 然后判断OK或者ERROR
static void AT_CIMI_MACHINE(u8 data)
{
    static u8 rev_count = 0; //记录接收数据位数
    switch (g_s_rev_state)
    {
        case S0:
        {
            if (data=='I')
            {
                g_s_rev_state=S1;
                rev_count=0;
            }
        }
        break;

        case S1:
        {
            if (data != 'R')
            {
                if (data != 'K')
                {
                    if ((data >= '0') && (data <= '9'))
                    {
                        buf_imsi[rev_count++] = data;
                    }
                }
                else
                {
                    rev_count = 0;
                    buf_imsi[15] = 0;
                    g_s_rev_state=S0;
                    Uart_Comm.rev_ack_flag = UART_OK;
                }
            }
            else
            {
                rev_count = 0;
                g_s_rev_state=S0;
                Uart_Comm.rev_ack_flag = UART_ERROR;
            }
        }
        break;

        default:
        {
            g_s_rev_state=S0;
            rev_count = 0;
            Uart_Comm.rev_ack_flag = UART_ERROR;
        }
        break;
    }
}

//Y7028读取数据
//+NSORF:0,47.104.217.235,5005,32,FEFE681001220522203378D603108D9F7EBB8321616E987E7FBBEF70711C8A16,0
#define AT_CHECK_LEN_NSORF   7
static const u8 scAT_NSORF[AT_CHECK_LEN_NSORF]="+NSORF:";
#define REV_STATE_NSORF_REV_TYPE   0x10 //判断数据来源
#define REV_STATE_NSORF_REV_LEN_S0    0x11 //接收数据长度
#define REV_STATE_NSORF_REV_LEN_S1    0x12 //接收数据长度
#define REV_STATE_NSORF_REV_LEN_S2    0x13 //接收数据长度
#define REV_STATE_NSORF_REV_LEN_S3    0x14 //接收数据长度
#define REV_STATE_NSORF_REV_DATA   0x15 //接收数据

static void AT_NBIOT_READ_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_NSORF-1):
		 {
			 g_s_rev_state_com = (scAT_NSORF[g_s_rev_state_com] == data? (REV_STATE_NSORF_REV_TYPE) : 0); 
			 break;
		 }
		 case(REV_STATE_NSORF_REV_TYPE):
		 {
			 Uart_Comm.rev_len = 0;
			 udp_rev_data_len =0;
			 g_s_udp_socket = NbiotGetSocketCode();
			 if(g_s_udp_socket == data)
			 {
				 Uart_Comm.nb_data_type = NB_RCV_UDP1_DATA;
				 g_s_rev_state_com = REV_STATE_NSORF_REV_LEN_S0;
			 }
			 else
			 {
				g_s_rev_state_com = 0;
			 }
			break;
		 }
		 case(REV_STATE_NSORF_REV_LEN_S0):
		 {
			 g_s_rev_state_com = (',' == data? (REV_STATE_NSORF_REV_LEN_S1) : REV_STATE_NSORF_REV_LEN_S0); 
			 break;
		 }
		 case(REV_STATE_NSORF_REV_LEN_S1):
		 {
			 g_s_rev_state_com = (',' == data? (REV_STATE_NSORF_REV_LEN_S2) : REV_STATE_NSORF_REV_LEN_S1); 
			 break;
		 }
		 case(REV_STATE_NSORF_REV_LEN_S2):
		 {
			 g_s_rev_state_com = (',' == data? (REV_STATE_NSORF_REV_LEN_S3) : REV_STATE_NSORF_REV_LEN_S2); 
			 break;
		 }
		 case (REV_STATE_NSORF_REV_LEN_S3):
		 {
			 if ((data >= '0') && (data <= '9'))
			 {
				 udp_rev_data_len *= 10;
				 udp_rev_data_len += (data -0x30);
			 }
			 else if(','== data)
			 {
				if(udp_rev_data_len < ML302_UP_BUF_SIZE * 2)
				{
					g_s_rev_state_com = REV_STATE_NSORF_REV_DATA;
				}
				else
				{
					g_s_rev_state_com = 0;
				}
			 }
			 break;
		 }
		 case (REV_STATE_NSORF_REV_DATA):
		 {
			if ((Uart_Comm.rev_len < (udp_rev_data_len*2))&&(','!= data))
			{   
				Uart_Comm.rev_buf[Uart_Comm.rev_len++] = data;
			}
			else
			{
				Uart_Comm.nbiot_rev_flag = TRUE; 
                g_s_rev_state_com = 0;
			}
			 break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_NSORF)
			{
				g_s_rev_state_com = (scAT_NSORF[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
AT+CSQ
+CSQ: 14,99
*/
#define AT_CHECK_LEN_CSQ   6
static const u8 scAT_CSQ[AT_CHECK_LEN_CSQ]="+CSQ: ";
static void AT_CSQ_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_CSQ-1):
		 {
			if (data != ',')
			{
				if (data >= '0' && data <= '9') //数字
				{
					g_csq_value *= 10;
					g_csq_value += (data - 0x30);
					g_s_rev_state_com = AT_CHECK_LEN_CSQ-1;
				}
			}
			else
			{
				Uart_Comm.rev_ack_flag = UART_OK;
				g_s_rev_state_com = 0;
			}
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_CSQ)
			{
				g_s_rev_state_com = (scAT_CSQ[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
				g_csq_value = (':' == data? (0) : g_csq_value); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
+NSONMI:0,32
*/
#define AT_CHECK_LEN_NSONMI   8
static const u8 scAT_NSONMI[AT_CHECK_LEN_NSONMI]="+NSONMI:";
static void AT_NSONMI_MACHINE(u8 data) 
{
	 switch (g_s_rev_socket_len_state)
	 {
		 case(AT_CHECK_LEN_NSONMI):
		 {
			/*if(data==g_s_tcp_socket)
			{
				Uart_Comm.rev_ack_flag = UART_OK;
				Uart_Comm.tcp_rev_data_hint = TRUE;//UDP收到数据提示
			}
			else 
			 */
			g_s_udp_socket = NbiotGetSocketCode();
			if(data==g_s_udp_socket)
			{
				Uart_Comm.rev_ack_flag = UART_OK;
				Uart_Comm.udp_rev_data_hint = TRUE;//UDP收到数据提示
			}
			else
			{
				g_s_rev_socket_len_state = 0;
			}
			break;
		 }
		 default:
		 {
			if(g_s_rev_socket_len_state<AT_CHECK_LEN_NSONMI)
			{
				g_s_rev_socket_len_state = (scAT_NSONMI[g_s_rev_socket_len_state] == data? (g_s_rev_socket_len_state+1) : 0); 
			}
			else
			{
				g_s_rev_socket_len_state = 0;
			}
			break;
		 }
	 }
}

/**
[14:36:32.887]收←◆AT+TUEINFO
TUEINFO:DLEARFCN,41134
TUEINFO:PCID,86
TUEINFO:RSRP,42
TUEINFO:RSRQ,17
TUEINFO:SINR,43
TUEINFO:MCL,112
TUEINFO:ULMCS,65535
TUEINFO:DLMCS,65535
TUEINFO:MPDCCHREPNUM,65535
TUEINFO:PUSCHREPNUM,65535
TUEINFO:PDSCHREPNUM,65535
TUEINFO:ULINITIALBLER,65535
TUEINFO:DLINITIALBLER,65535
TUEINFO:ULRBNUM,4294967295
TUEINFO:DLRBNUM,4294967295
TUEINFO:ULRLCRATE,4294967295
TUEINFO:DLRLCRATE,4294967295
TUEINFO:ULTBS,4294967295
TUEINFO:DLTBS,4294967295
TUEINFO:ECL,0

OK
*/
#define AT_CHECK_LEN_TUEINFO_RSRP   5
#define AT_CHECK_LEN_TUEINFO_SINR   5
#define AT_CHECK_LEN_TUEINFO_ECL    4
static const u8 scAT_TUEINFO_RSRP[AT_CHECK_LEN_TUEINFO_RSRP]="RSRP,";
static const u8 scAT_TUEINFO_SINR[AT_CHECK_LEN_TUEINFO_SINR]="SINR,";
static const u8 scAT_TUEINFO_ECL[AT_CHECK_LEN_TUEINFO_ECL]="ECL,";
#define INFO_STATE_RSRP  0X10
#define INFO_STATE_SINR  0X20
#define INFO_STATE_ECL   0X30
#define INFO_STATE_ERR_WT 0X40 //错误等待

u8 rev_state_buf[4];
static void AT_TUEINFO_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(INFO_STATE_RSRP)://读取RSRP
		 {
			if (data != '\r')
			{
				if (data >= '0' && data <= '9') //数字
				{
					s_g_rsrp_value *= 10;
					s_g_rsrp_value += (data - 0x30);
					g_s_rev_state_com = INFO_STATE_RSRP;
				}
				if(data =='-')//防止出现“-1”，TUEINFO:RSRP,-1
				{
					g_s_rev_state_com = INFO_STATE_ERR_WT;
				}
			}
			else
			{
				s_g_rsrp_value = (s_g_rsrp_value - 140)*10;
				g_s_rev_state_com = 0;
			}
			break;
		 }
		 
		 case(INFO_STATE_SINR)://读取SINR
		 {
			if (data != '\r')
			{
				if (data >= '0' && data <= '9') //数字
				{
					s_g_snr_value *= 10;
					s_g_snr_value += (data - 0x30);
					g_s_rev_state_com = INFO_STATE_SINR;
				}
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
		 case(INFO_STATE_ECL)://读取ECL
		 {
			if (data != '\r')
			{
				if (data >= '0' && data <= '9') //数字
				{
					s_g_ecl_value *= 10;
					s_g_ecl_value += (data - 0x30);
					g_s_rev_state_com = INFO_STATE_ECL;
				}
			}
			else
			{
				Uart_Comm.rev_ack_flag = UART_OK;
				rev_state_buf[0] = 0;
				rev_state_buf[1] = 0;
				rev_state_buf[2] = 0;
				rev_state_buf[3] = 0;
				g_s_rev_state_com = 0;
			}
			break;
		 }
		 case(INFO_STATE_ERR_WT)://等待超时
		 {
			 break;
		 }
		 default:
		 {
			if(rev_state_buf[0]<AT_CHECK_LEN_TUEINFO_RSRP)
			{
				rev_state_buf[0] = ((scAT_TUEINFO_RSRP[rev_state_buf[0]] == data)? (rev_state_buf[0]+1) : 0); 
				s_g_rsrp_value = ((AT_CHECK_LEN_TUEINFO_RSRP== rev_state_buf[0])? (0) : s_g_rsrp_value);
				g_s_rev_state_com = (AT_CHECK_LEN_TUEINFO_RSRP == rev_state_buf[0]? (INFO_STATE_RSRP) : g_s_rev_state_com);				
			}
			if(rev_state_buf[1]<AT_CHECK_LEN_TUEINFO_SINR)
			{
				rev_state_buf[1] = ((scAT_TUEINFO_SINR[rev_state_buf[1]] == data)? (rev_state_buf[1]+1) : 0); 
				s_g_snr_value = ((AT_CHECK_LEN_TUEINFO_SINR== rev_state_buf[1])? (0) : s_g_snr_value);
				g_s_rev_state_com = (AT_CHECK_LEN_TUEINFO_SINR == rev_state_buf[1]? (INFO_STATE_SINR) : g_s_rev_state_com);				
			}
			if(rev_state_buf[2]<AT_CHECK_LEN_TUEINFO_ECL)
			{
				rev_state_buf[2] = ((scAT_TUEINFO_ECL[rev_state_buf[2]] == data)? (rev_state_buf[2]+1) : 0); 
				s_g_ecl_value = ((AT_CHECK_LEN_TUEINFO_ECL== rev_state_buf[2])? (0) : s_g_ecl_value);
				g_s_rev_state_com = (AT_CHECK_LEN_TUEINFO_ECL == rev_state_buf[2]? (INFO_STATE_ECL) : g_s_rev_state_com);				
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
[16:52:55.548]收←◆AT+CCED=0,1
+CCED:LTE current cell:460,00,460045858412892,0,40,n100,38950,74802246,21,20,21272,8,333
*/
#define AT_CHECK_LEN_CCED        5
#define AT_CHECK_LEN_CCED_BAND   4
#define AT_CHECK_LEN_CCED_CELLID 2
static const u8 scAT_CCED[AT_CHECK_LEN_CCED]="cell:";
static const u8 scAT_CCED_BAND[AT_CHECK_LEN_CCED]=",,,,";
static const u8 scAT_CCED_CELLID[AT_CHECK_LEN_CCED_CELLID]=",,";
#define CCED_STATE_BAND_POS    0X10
#define CCED_STATE_BAND_DAT    0X20
#define CCED_STATE_CELLID_POS  0X30
#define CCED_STATE_CELLID_DAT  0X40
static void AT_CCED_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(CCED_STATE_BAND_POS)://band位置判断
		 {
			if(rev_state_buf[1]<AT_CHECK_LEN_CCED_BAND)
			{
				rev_state_buf[1] = ((scAT_CCED_BAND[rev_state_buf[1]] == data)? (rev_state_buf[1]+1) : rev_state_buf[1]); 
				s_g_band_value = ((AT_CHECK_LEN_CCED_BAND== rev_state_buf[1])? (0) : s_g_band_value);
				g_s_rev_state_com = (AT_CHECK_LEN_CCED_BAND == rev_state_buf[1]? (CCED_STATE_BAND_DAT) : g_s_rev_state_com);				
			}
			break;
		 }
		 
		 case(CCED_STATE_BAND_DAT)://读取band
		 {
			if (data != ',')
			{
				if (data >= '0' && data <= '9') //数字
				{
					s_g_band_value *= 10;
					s_g_band_value += (data - 0x30);
					rev_state_buf[1] = 0;
				}
			}
			else
			{
				g_s_rev_state_com = CCED_STATE_CELLID_POS;
			}
			break;
		 }
		 case(CCED_STATE_CELLID_POS)://cill id 位置判断
		 {
			if(rev_state_buf[1]<AT_CHECK_LEN_CCED_CELLID)
			{
				rev_state_buf[1] = ((scAT_CCED_CELLID[rev_state_buf[1]] == data)? (rev_state_buf[1]+1) : rev_state_buf[1]); 
				g_s_rev_state_com = (AT_CHECK_LEN_CCED_CELLID == rev_state_buf[1]? (CCED_STATE_CELLID_DAT) : g_s_rev_state_com);	
				rev_state_buf[2] = 0;				
			}
			break;
		 }
		 case(CCED_STATE_CELLID_DAT)://读取cill id
		 {
			if (data != ',')
			{
				if (data >= '0' && data <= '9') //数字
				{
					if(rev_state_buf[2]<CELL_ID_SIZE)
					{
						s_g_cell_id[rev_state_buf[2]++] = data;
					}
				}
			}
			else
			{
				if((s_g_band_value > 0)&&(s_g_band_value<42))//防止出现这种情况：+CCED:LTE current cell:000,000,460045858412907,0,0,NULL,-1,0,0,0,0,0,0
				{
					Uart_Comm.rev_ack_flag = UART_OK;
				}
				rev_state_buf[0] = 0;
				rev_state_buf[1] = 0;
				rev_state_buf[2] = 0;
				rev_state_buf[3] = 0;
				g_s_rev_state_com = 0;
			}
			break;
		 }
		 default:
		 {
			if(rev_state_buf[0]<AT_CHECK_LEN_CCED)
			{
				rev_state_buf[0] = ((scAT_CCED[rev_state_buf[0]] == data)? (rev_state_buf[0]+1) : 0); 
				g_s_rev_state_com = (AT_CHECK_LEN_CCED== rev_state_buf[0]? (CCED_STATE_BAND_POS) : g_s_rev_state_com);
				rev_state_buf[1] = 0;				
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}


void NbiotGetECL(s16 *ecl_data)
{
    *ecl_data = s_g_ecl_value;
}

//询问IMEI   命令为：AT+CGSN=1  正确应答格式：+CGSN:865820031203206  OK

static void AT_IMEI_MACHINE(u8 data)
{
    static u8 rev_count = 0; //记录接收数据位数
    switch (g_s_rev_state)
    {
        case S0:
        {
            if (data == '+')
            {
                g_s_rev_state = S1;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S1:
        {
            if (data == 'C')
            {
                g_s_rev_state = S2;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S2:
        {
            if (data == 'G')
            {
                g_s_rev_state = S3;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S3:
        {
            if (data == 'S')
            {
                g_s_rev_state = S4;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S4:
        {
            if (data == 'N')
            {
                g_s_rev_state = S5;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S5:
        {
            if (data == ':')
            {
                g_s_rev_state = S6;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S6:
        {
            if (data != 'K')
            {
                if ((data >= '0') && (data <= '9'))
                {
                    buf_imei[rev_count++] = data;
                }
            }
            else
            {
                rev_count = 0;
                g_s_rev_state = S0;
                buf_imei[15] = 0;
                Uart_Comm.rev_ack_flag = UART_OK;
            }
        }
        break;

        default:
        {
            g_s_rev_state = S0;
        }
        break;
    }
}

void NbiotGetIMEI(char *imei)
{
    u8 i = 0;
    for (i = 0; i < 15; i++)
    {
        *(imei + i) = buf_imei[i];
    }
}
void NbiotGetIMSI(char *imsi)
{
    u8 i = 0;
    for (i = 0; i < 15; i++)
    {
        *(imsi + i) = buf_imsi[i];
    }
}
void NbiotGetICCID(char *iccid)
{
    u8 i = 0;
    for (i = 0; i < 20; i++)
    {
        *(iccid + i) = buf_iccid[i];
    }
}
/**
AT+CCLK?
+CCLK: "21/09/15,09:47:26+32"
+CCLK: "21/05/13,16:37:30"
*/
//+CCLK:22/05/23,15:15:46+32
static void AT_GET_CCLK_MACHINE(u8 data)
{
    static u8 get_cclk_state = S0;
    static u8 temp_data;
    switch (get_cclk_state)
    {
        case S0:
        {
            if (data == '+')
            {
                get_cclk_state = S1;
            }
        }
        break;

        case S1:
        {
            if (data == 'C')
            {
                get_cclk_state = S2;
            }
            else
            {
                get_cclk_state = S0;
            }
        }
        break;

        case S2:
        {
            if (data == 'C')
            {
                get_cclk_state = S3;
            }
            else
            {
                get_cclk_state = S0;
            }
        }
        break;

        case S3:
        {
            if (data == 'L')
            {
                get_cclk_state = S4;
            }
            else
            {
                get_cclk_state = S0;
            }
        }
        break;

        case S4:
        {
            if (data == 'K')
            {
                get_cclk_state = S5;
            }
            else
            {
                get_cclk_state = S0;
            }
        }
        break;

        case S5:
        {
            if (data == ':')
            {
                get_cclk_state = S6;
                temp_data = 0;
            }
        }
        break;

        case S6://获取年
        {
            if (data != '/')
            {
                if (data >= '0' && data <= '9')
                {
                    temp_data = temp_data * 10;
                    temp_data += (data - 0x30);
                }
            }
            else
            {
                tm_GMT_nb_uart_driver.tm_year = (2000 - 1900) + temp_data; //从1900算起
                temp_data = 0;
                get_cclk_state = S7;
            }
        }
        break;

        case S7://获取月
        {
            if (data != '/')
            {
                if (data >= '0' && data <= '9')
                {
                    temp_data = temp_data * 10;
                    temp_data += (data - 0x30);
                }
            }
            else
            {
                tm_GMT_nb_uart_driver.tm_mon = temp_data - 1; //月范围是0-11 调整月
                temp_data = 0;
                get_cclk_state = S8;
            }
        }
        break;

        case S8://获取日
        {
            if (data != ',')
            {
                if (data >= '0' && data <= '9')
                {
                    temp_data = temp_data * 10;
                    temp_data += (data - 0x30);
                }
            }
            else
            {
                tm_GMT_nb_uart_driver.tm_mday = temp_data;
                temp_data = 0;
                get_cclk_state = S9;
            }
        }
        break;

        case S9://获取时
        {
            if (data != ':')
            {
                if (data >= '0' && data <= '9')
                {
                    temp_data = temp_data * 10;
                    temp_data += (data - 0x30);
                }
            }
            else
            {
                tm_GMT_nb_uart_driver.tm_hour = temp_data;
                temp_data = 0;
                get_cclk_state = S10;
            }
        }
        break;

        case S10://获取分
        {
            if (data != ':')
            {
                if (data >= '0' && data <= '9')
                {
                    temp_data = temp_data * 10;
                    temp_data += (data - 0x30);
                }
            }
            else
            {
                tm_GMT_nb_uart_driver.tm_min = temp_data;
                temp_data = 0;
                get_cclk_state = S11;
            }
        }
        break;

        case S11://获取秒
        {
            if (data != '+')
            {
                if (data >= '0' && data <= '9')
                {
                    temp_data = temp_data * 10;
                    temp_data += (data - 0x30);
                }
            }
            else
            {
                tm_GMT_nb_uart_driver.tm_sec = temp_data;
                static DateTime rtcc_tmp; //RTC设置数据格式 BCD
                tm_GMT_nb_uart_driver.tm_year -= (2000 - 1900);
                tm_GMT_nb_uart_driver.tm_mon += 1; //月范围(0-11)  +1调整到(1-12)
                rtcc_tmp.s.second = ((tm_GMT_nb_uart_driver.tm_sec / 10) << 4) | (tm_GMT_nb_uart_driver.tm_sec % 10);
                rtcc_tmp.s.minute = ((tm_GMT_nb_uart_driver.tm_min / 10) << 4) | (tm_GMT_nb_uart_driver.tm_min % 10);
                rtcc_tmp.s.hour = ((tm_GMT_nb_uart_driver.tm_hour / 10) << 4) | (tm_GMT_nb_uart_driver.tm_hour % 10);
                rtcc_tmp.s.day = ((tm_GMT_nb_uart_driver.tm_mday / 10) << 4) | (tm_GMT_nb_uart_driver.tm_mday % 10);
                rtcc_tmp.s.month = ((tm_GMT_nb_uart_driver.tm_mon / 10) << 4) | (tm_GMT_nb_uart_driver.tm_mon % 10);
                rtcc_tmp.s.year = ((tm_GMT_nb_uart_driver.tm_year / 10) << 4) | (tm_GMT_nb_uart_driver.tm_year % 10);
				if(rtcc_tmp.s.year>=0x22)
				{
					 RtccSetDateTime(&rtcc_tmp);
					 RtccAdjustDateTime(1,3600*8);//时,UTC时间比北京时间慢8小时
				}
                temp_data = 0;
                get_cclk_state = S0;
                Uart_Comm.rev_ack_flag = UART_OK;
            }
        }
        break;

        default:
        {
            get_cclk_state = S0;
        }
        break;
    }
}
/**
//查询当前SIM卡的ICCID号,+NCCID:4412345678901234567\r\n
*/
#define AT_CHECK_LEN_ICCID   7
static const u8 scAT_ICCID[AT_CHECK_LEN_ICCID]="+NCCID:";
static void AT_ICCID_MACHINE(u8 data)
{
	  static u8 rev_count = 0;
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_ICCID-1):
		 {
			if (data != 'K')
			{
				if ((data >= '0' && data <= '9') || (data >= 'A' && data <= 'F')) //数字
				{
					g_s_rev_state_com = AT_CHECK_LEN_ICCID-1;
					if(rev_count<20)
					{
						buf_iccid[rev_count++] = data;
					}
					else
					{
						g_s_rev_state_com = 0;
					}
				}
			}
			else
			{
				rev_count = 0;
				buf_iccid[20] = 0;
				Uart_Comm.rev_ack_flag = UART_OK;
				g_s_rev_state_com = 0;
			}
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_ICCID)
			{
				g_s_rev_state_com = (scAT_ICCID[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
				rev_count = 0;
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}

////查询当前SIM卡的ICCID号,+NCCID:4412345678901234567\r\n
//static void AT_ICCID_MACHINE(u8 data)
//{
//    static u8 rev_count = 0;
//    switch (g_s_rev_state)
//    {
//    case S0:
//    {
//        if (data == 'I')//"I"
//        {
//            g_s_rev_state = S1;
//        }
//        else
//        {
//            g_s_rev_state = S0;
//        }
//    }
//    break;

//    case S1:
//    {
//        if (data == 'D')//"D"
//        {
//            g_s_rev_state = S2;
//        }
//        else
//        {
//            g_s_rev_state = S1;
//        }
//    }
//    break;

//    case S2:
//    {
//        if (data == ':')//":"
//        {
//            rev_count = 0;
//            g_s_rev_state = S3;
//        }
//    }
//    break;

//    case S3:
//    {
//        if (data != 'R')
//        {
//            if (data != 'K')
//            {
//						if ((data >= '0' && data <= '9') || (data >= 'A' && data <= 'Z') || (data >= 'a' && data <= 'z')) //数字
//                {
//							Nbiot_three_code.iccid[rev_count++] = data;
//                }
//            }
//            else
//            {
//                rev_count = 0;
//						Nbiot_three_code.iccid[20] = 0;
//                Uart_Comm.rev_ack_flag = UART_OK;
//            }
//        }
//        else
//        {
//            rev_count = 0;
//            Uart_Comm.rev_ack_flag = UART_ERROR;
//        }
//    }
//    break;

//    default:
//    {
//        g_s_rev_state = S0;
//    }
//    break;
//    }
//}
/**
AT+CPIN?
+CPIN: READY
*/
//#define AT_CHECK_LEN_CPIN   12
//static const u8 scAT_CPIN[AT_CHECK_LEN_CPIN]="+CPIN: READY";
#define AT_CHECK_LEN_CPIN   11
static const u8 scAT_CPIN[AT_CHECK_LEN_CPIN]="+CPIN:READY";
static void AT_CPIN_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_CPIN-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == 'Y')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_CPIN)
			{
				g_s_rev_state_com = (scAT_CPIN[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
AT+MIPOPEN=1,"TCP","58.59.6.130",21628
OK
1,CONNECT OK

AT+MIPOPEN=1,"TCP","58.59.6.130",21628
1,ALREADY CONNECT
*/
#define AT_CHECK_LEN_MIPOPEN   10
static const u8 scAT_MIPOPEN1[AT_CHECK_LEN_MIPOPEN]="CONNECT OK";
static const u8 scAT_MIPOPEN2[AT_CHECK_LEN_MIPOPEN]="ALREADY CO";
u8 tcp_socket_code='0'; //0..5  A numeric parameter which indicates the connection number
static void AT_MIPOPEN_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_MIPOPEN-1):
		 {
			Uart_Comm.rev_ack_flag = (((data == 'K')||((data == 'O')))? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_MIPOPEN)
			{
				g_s_rev_state_com = (((scAT_MIPOPEN1[g_s_rev_state_com] == data)||(scAT_MIPOPEN2[g_s_rev_state_com] == data))? (g_s_rev_state_com+1) : 0); 
				tcp_socket_code = (((data >= '0')&&(data<='5'))? data : '0');
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
AT+MIPSEND=1,0352
> 
AT+MIPSEND=1,0352
AT+MIPSEND=1,0352
*/
#define AT_CHECK_LEN_MIPSENDCOUNT   1
static const u8 scAT_MIPSENDCOUNT[AT_CHECK_LEN_MIPSENDCOUNT]=">";
static void AT_MIPSENDCOUNT_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_MIPSENDCOUNT)
			{
				if(data == '>')//检测到'>'就直起ok
				{
					Uart_Comm.rev_ack_flag = UART_OK;
				}
				g_s_rev_state_com = ((scAT_MIPSENDCOUNT[g_s_rev_state_com] == data)?0:0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/*
[16:19:01.482]收←◆AT+MIPSEND=1,0352
> 
[16:19:02.586]收←◆FEFE6810010000000033786703A0B267F17F4A63B7EE0B662AFC7620693B00DD25A695A747056D041A980FB1420C6AD4455224C8B6B4331F810C8AF16ECD00DD25A695A747056D041A980FB1420C00DD25A695A747056D041A980FB1420C00DD25A695A747056D041A980FB1420C00DD25A695A747056D041A980FB1420C00DD25A695A747056D041A980FB1420C74BCF5A165D6EF5B1EBBA33402A7AA8D4483DE918F0F9D2040E3DA3FA8CE5C268016
[16:19:03.210]收←◆
1,SEND OK
*/
#define AT_CHECK_LEN_MIPSEND   7
static const u8 scAT_MIPSEND[AT_CHECK_LEN_MIPSEND]="SEND OK";
static void AT_MIPSEND_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_MIPSEND-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == 'K')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_MIPSEND)
			{
				g_s_rev_state_com = ((scAT_MIPSEND[g_s_rev_state_com] == data)? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/*
AT+MIPCLOSE=1
1,CLOSE OK
*/
#define AT_CHECK_LEN_MIPCLOSE   8
static const u8 scAT_MIPCLOSE[AT_CHECK_LEN_MIPCLOSE]="CLOSE OK";
static void AT_MIPCLOSE_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_MIPCLOSE-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == 'K')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_MIPCLOSE)
			{
				g_s_rev_state_com = ((scAT_MIPCLOSE[g_s_rev_state_com] == data)? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/*
AT+VERCTRL?
+VERCTRL: 0, 1
*/
#define AT_CHECK_LEN_VERCTRL_CK   14
static const u8 scAT_VERCTRL_CK[AT_CHECK_LEN_VERCTRL_CK]="+VERCTRL: 0, 1";
static void AT_VERCTRL_CK_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_VERCTRL_CK-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == '1')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_VERCTRL_CK)
			{
				g_s_rev_state_com = ((scAT_VERCTRL_CK[g_s_rev_state_com] == data)? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}

/**
查询发射功率值
+MAXULPOW:0   //不降低
+MAXULPOW:22  //降低
*/
#define AT_CHECK_LEN_NV_CK   11
static const u8 scAT_NV_CK[AT_CHECK_LEN_NV_CK]="+MAXULPOW:0";
static void AT_NV_CK_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_NV_CK-1):
		 {
			 if(Sys_data_object.P1.func_cfg_bytes[11] == 0x55)
			 {
				Uart_Comm.rev_ack_flag = ((data == '2')? UART_OK : UART_ERROR);
			 }
			 else
			 {
				Uart_Comm.rev_ack_flag = ((data == '0')? UART_OK : UART_ERROR);
			 }
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_NV_CK)
			{
				g_s_rev_state_com = ((scAT_NV_CK[g_s_rev_state_com] == data)? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
模组重启
+MODULE_READY:2
*/
#define AT_CHECK_LEN_NRB   15
static const u8 scAT_NRB_CK[AT_CHECK_LEN_NRB]="+MODULE_READY:2";
static void AT_NRB_CK_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_NRB-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == '2')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_NRB)
			{
				g_s_rev_state_com = ((scAT_NRB_CK[g_s_rev_state_com] == data)? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}

/*正常注册
[14:27:10.106]收←◆
+MCWEVENT:1
[14:27:11.418]收←◆
+MCWEVENT:7

没有注册的情况
+MCWEVENT:2
*/
static void AT_MCWOPEN_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {
    case S0:
    {
        if (data == 'E') g_s_rev_state = S1;
        else g_s_rev_state = S0;
    }
    break;

    case S1:
    {
        if (data == 'V') g_s_rev_state = S2;
        else g_s_rev_state = S0;
    }
    break;

    case S2:
    {
        if (data == 'E') g_s_rev_state = S3;
        else g_s_rev_state = S0;
    }
    break;

    case S3:
    {
        if (data == 'N') g_s_rev_state = S4;
        else g_s_rev_state = S0;
    }
    break;

    case S4:
    {
        if (data == 'T') g_s_rev_state = S5;
        else g_s_rev_state = S0;
    }
    break;
    case S5:
    {
        if (data == ':') g_s_rev_state = S6;
        else g_s_rev_state = S0;
    }
    break;
    case S6:
    {
        if (data == '7')//7 订阅成功
        {
            Uart_Comm.rev_ack_flag = UART_OK;
            g_s_rev_state = S0; //检测到结果,置空状态
        }
		else if(data=='2'||data=='8')//注册/订阅失败
		{
			Uart_Comm.rev_ack_flag = UART_ERROR;
			g_s_rev_state = S0; //检测到结果,置空状态
		}
        else
        {
            g_s_rev_state = S0; 
        }
    }
    break;
    default:
    {
        g_s_rev_state = S0;
    }
    break;
    }
}

static void AT_MCWSEND_ACK_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {
    case S0:
    {
        if (data == 'E') g_s_rev_state = S1;
        else g_s_rev_state = S0;
    }
    break;

    case S1:
    {
        if (data == 'V') g_s_rev_state = S2;
        else g_s_rev_state = S0;
    }
    break;

    case S2:
    {
        if (data == 'E') g_s_rev_state = S3;
        else g_s_rev_state = S0;
    }
    break;

    case S3:
    {
        if (data == 'N') g_s_rev_state = S4;
        else g_s_rev_state = S0;
    }
    break;

    case S4:
    {
        if (data == 'T') g_s_rev_state = S5;
        else g_s_rev_state = S0;
    }
    break;
    case S5:
    {
        if (data == ':') g_s_rev_state = S6;
        else g_s_rev_state = S0;
    }
    break;
    case S6://只检测0,6
    {
        if (data == '9')
        {
//				if(PAYMENT_METER == Sys_data_object.Meter_function_cfg_8000.payment_form)
//				{
//					SetCommuOverFlag_IES();
//				}
            Uart_Comm.rev_ack_flag = UART_OK;
            g_s_rev_state = S0; //检测到结果,置空状态
        }
        else
        {
            g_s_rev_state = S0; //Bootstrap 流程处理成功 继续等待

        }
    }
    break;
    default:
    {
        g_s_rev_state = S0;
    }
    break;
    }
}

//+MCWREAD:32,FEFE68100130032120337840011017C7F8999259CBA6B744B394C94B9DB96016,0
static void AT_NBIOT_REV_MACHINE_MN316_CTWing(u8 data)
{
    switch (g_s_rev_state_coap)
    {
    case S0:
    {
        if (data == 'R')
        {
            g_s_rev_state_coap = S2;
        }
        else
        {
            g_s_rev_state_coap = S0;
        }
    }
    break;

    case S2:
    {
        if (data == 'E')
        {
            g_s_rev_state_coap = S3;
        }
        else
        {
            g_s_rev_state_coap = S0;
        }
    }
    break;

    case S3:
    {
        if (data == 'A')
        {
            g_s_rev_state_coap = S4;
        }
        else
        {
            g_s_rev_state_coap = S0;
        }
    }
    break;

    case S4:
    {
        if (data == 'D')
        {
            g_s_rev_state_coap = S5;
        }
        else
        {
            g_s_rev_state_coap = S0;
        }
    }
    break;

    case S5://
    {
        if (data == ':')
        {
            Uart_Comm.nbiot_rev_serial_number_cnt = 0;
            g_s_rev_state_coap = S6;
        }
        else
        {
            g_s_rev_state_coap = S0;
        }
    }
    break;

    case S6:
    {
        if (data != ',')
        {
            if(Uart_Comm.nbiot_rev_serial_number_cnt >= sizeof(Uart_Comm.nbiot_rev_serial_number))
            {
                Uart_Comm.nbiot_rev_serial_number_cnt = 0;
                g_s_rev_state_coap = S0; //异常--剩下的数据也不要了
                break;
            }
            if (data >= 0x30 && data <= 0x39)
            {
                Uart_Comm.nbiot_rev_serial_number[Uart_Comm.nbiot_rev_serial_number_cnt++] = data;
            }
        }
        else
        {
            Uart_Comm.rev_len = 0;
            Uart_Comm.nbiot_rev_serial_number_cnt = 0;
            g_s_rev_state_coap = S7; //第2个','
        }
    }
    break;

    case S7:
    {
        if ((data != ',')&&(data != 0x0D))//  ',' 或者 回车
        {
            if (Uart_Comm.rev_len < UP_BUF_SIZE * 2)
            {
                Uart_Comm.flow_count++; //流量按字节累加
                if ((data >= 0x30 && data <= 0x39) || (data >= 0x41 && data <= 0x46) || (data >= 0x61 && data <= 0x66))
                {   //只选择数字0-9 字符 A-F 或者 a-f
                    Uart_Comm.rev_buf[Uart_Comm.rev_len++] = data;
                }
            }
            else
            {
                Uart_Comm.rev_len = 0;
                g_s_rev_state_coap = S0;
            }
        }
        else
        {
            Uart_Comm.nb_data_type = NB_RCV_COAP_DATA;
            SetUartCommRevFlag();
            //Uart_Comm.hex_flag = DATATYPE_CHAR;
            g_s_rev_state_coap = S0;
        }
    }
    break;

    default:
    {
        g_s_rev_state_coap = S0;
    }
    break;
    }
}

static void AT_NBIOT_REV_MACHINE_MN316_ONENET(u8 data)
{
    switch (g_s_rev_state_onenet)
    {
    case 0:
    case S0:
    {
        if (data == '+')
        {
            g_s_rev_state_onenet = S1;
        }
    }
    break;
    case S1:
    {
        if (data == 'M')
        {
            g_s_rev_state_onenet = S2;
        }
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S2:
    {
        if (data == 'I')
        {
            g_s_rev_state_onenet = S3;
        }
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S3:
    {
        if (data == 'P')
        {
            g_s_rev_state_onenet = S4;
        }
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S4:
    {
        if (data == 'L')
        {
            g_s_rev_state_onenet = S5;
        }
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S5://采用 +MIPLWRITE下发的指令
    {
        if (data == 'W') //采用 +MIPLWRITE下发的指令
        {
            Uart_Comm.Onenet_type_down_order = ORDER_WRITE;
            g_s_rev_state_onenet = S6;
        }
        else if (data == 'E') //采用 +MIPLEXCUTE下发的指令
        {
            Uart_Comm.Onenet_type_down_order = ORDER_EXECUTE;
            g_s_rev_state_onenet = S6_1;
        }
        else g_s_rev_state_onenet=S0;
    }
    break;
    /*------------------------++MIPLWRITE流程分支---------------------------*/
    case S6:
    {
        if (data == 'R') g_s_rev_state_onenet = S7;
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S7:
    {
        if (data == 'I') g_s_rev_state_onenet = S8;
        else g_s_rev_state_onenet=S0;
    }
    break;
    /*------------------------+MIPLEXCUTE流程分支---------------------------*/
    case S6_1:
    {
        if (data == 'X') g_s_rev_state_onenet = S7_1;
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S7_1:
    {
        if (data == 'E') g_s_rev_state_onenet = S8_1;
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S8_1:
    {
        if (data == 'C') g_s_rev_state_onenet = S9_1;
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S9_1:
    {
        if (data == 'U') g_s_rev_state_onenet = S8;
        else g_s_rev_state_onenet=S0;
    }
    break;
    /*-------------------------两个流程在此处会师---------------------------------------*/
    case S8:
    {
        if (data == 'T')  g_s_rev_state_onenet = S9;
        else g_s_rev_state_onenet=S0;
    }
    break;

    case S9:
    {
        if (data == 'E')
        {
            g_s_rev_state_onenet = S10;
        }
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S10:
    {
        if (data == ':')
        {
            g_s_rev_state_onenet = S11;
        }
        else g_s_rev_state_onenet=S0;
    }
    break;
    case S11:
    {
        if (data == '0')
        {
            g_s_rev_state_onenet = S12;
        }
    }
    break;
    case S12:
    {
        if (data == ',')  //第1个','
        {
            g_s_rev_state_onenet = S13;
            Uart_Comm.nbiot_rev_serial_number_cnt=0;
            memset(Uart_Comm.nbiot_rev_serial_number,0,sizeof(Uart_Comm.nbiot_rev_serial_number));
        }
    }
    break;
    case S13:
    {
        if (data != ',')
        {
            if(Uart_Comm.nbiot_rev_serial_number_cnt >= sizeof(Uart_Comm.nbiot_rev_serial_number))
            {
                Uart_Comm.nbiot_rev_serial_number_cnt = 0;
                g_s_rev_state_onenet = S0; //异常--剩下的数据也不要了
                break;
            }
            if(data >= 0x30 && data <= 0x39)
            {
                Uart_Comm.nbiot_rev_serial_number[Uart_Comm.nbiot_rev_serial_number_cnt++]=data;
            }
        }
        else
        {
            Uart_Comm.nbiot_rev_serial_number_cnt = 0;
            g_s_rev_state_onenet = S14;  //第2个','
        }
    }
    break;
    case S14:
    {
        if (data == ',') //第3个','
        {
            if(Uart_Comm.Onenet_type_down_order == ORDER_WRITE)
                g_s_rev_state_onenet = S15;
            else if(Uart_Comm.Onenet_type_down_order == ORDER_EXECUTE ) //+MIPLEXCUTE 少一个‘,’跳过一个
                g_s_rev_state_onenet = S16;
        }

    }
    break;
    case S15:
    {
        if (data == ',')//第4个','
        {
            g_s_rev_state_onenet = S16;
        }
    }
    break;
    case S16:
    {
        if (data == ',') //第5个','
        {
            g_s_rev_state_onenet = S17;
        }
    }
    break;

    case S17:
    {
        if (data == ',')//第6个','
        {
            g_s_rev_state_onenet = S18;
        }
    }
    break;

    case S18:
    {
        if (data == ',')//第7个','
        {
            g_s_rev_state_onenet = S19;
            Uart_Comm.rev_len=0;
        }
    }
    break;

    case S19://+MIPLWRITE 以','结束    +MIPLEXCUTE以回车结束
    {
        if ((data != ',')&&(data != 0x0D))//  ',' 或者 回车
        {
            if (Uart_Comm.rev_len < UP_BUF_SIZE*2)
            {
                Uart_Comm.flow_count++; //流量按字节累加
                if((data >= 0x30 && data <= 0x39) || (data >= 0x41 && data <= 0x46) || (data >= 0x61 && data <= 0x66))
                {   //只选择数字0-9 字符 A-F 或者 a-f
                    Uart_Comm.rev_buf[Uart_Comm.rev_len++] = data;
                }
            }
            else
            {
                Uart_Comm.rev_len=0;
                g_s_rev_state_onenet = S0;
            }
        }
        else
        {
            Uart_Comm.nbiot_rev_ack = TRUE;//先做写应答然后处理数据
            SetUartCommRevFlag();
            //Uart_Comm.hex_flag = DATATYPE_CHAR;
            g_s_rev_state_onenet = S0;
        }
    }
    break;

    default:
    {
        g_s_rev_state_onenet = S0;
    }
    break;
    }
}

static void AT_NBIOT_REV_MACHINE(u8 data)
{
	AT_NBIOT_REV_MACHINE_MN316_ONENET(data);
	AT_NBIOT_REV_MACHINE_MN316_CTWing(data);
}	

static void AT_QIOPEN_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {

    case S0:
    {
        if (data == 'N')  g_s_rev_state = S1;
        else g_s_rev_state = S0;
    }
    break;

    case S1:
    {
        if (data == 'S')  g_s_rev_state = S2;
        else g_s_rev_state = S0;
    }
    break;

    case S2:
    {
        if (data == 'O')  g_s_rev_state = S3;
        else g_s_rev_state = S0;
    }
    break;

    case S3:
    {
        if (data == 'C')  g_s_rev_state = S4;
        else g_s_rev_state = S0;
    }
    break;

    case S4:
    {
        if (data == 'R')  g_s_rev_state = S5;
        else g_s_rev_state = S0;
    }
    break;

    case S5:
    {
        if (data == ':')  g_s_rev_state = S6;
        else g_s_rev_state = S0;
    }
    break;

    case S6:
    {
        if ((data >= '0')&& (data <= '9'))
        {
            tcp_socket_code = data;// - 0x30;
            Uart_Comm.rev_ack_flag = UART_OK;
            g_s_rev_state = S0; //检测到结果,置空状态
        }
        else g_s_rev_state = S0;
    }
    break;
    default:
    {
        g_s_rev_state = S0;
    }
    break;
    }
}
static void AT_MIPLCREATEEX(u8 data) //+MIPLCREATEEX:0
{
    switch (g_s_rev_state)
    {
    case S0:
    {
        if (data == 'C') g_s_rev_state = S1;
        else g_s_rev_state = S0;
    }
    break;

    case S1:
    {
        if (data == 'R') g_s_rev_state = S2;
        else g_s_rev_state = S0;
    }
    break;

    case S2:
    {
        if (data == 'E') g_s_rev_state = S3;
        else g_s_rev_state = S0;
    }
    break;

    case S3:
    {
        if (data == 'A') g_s_rev_state = S4;
        else g_s_rev_state = S0;
    }
    break;

    case S4:
    {
        if (data == 'T') g_s_rev_state = S5;
        else g_s_rev_state = S0;
    }
    break;
    case S5:
    {
        if (data == 'E') g_s_rev_state = S6;
        else g_s_rev_state = S0;
    }
    break;

    case S6:
    {
        if (data == 'E') g_s_rev_state = S7;
        else g_s_rev_state = S0;
    }
    break;

    case S7:
    {
        if (data == 'X') g_s_rev_state = S8;
        else g_s_rev_state = S0;
    }
    break;
    case S8:
    {
        if (data == ':') g_s_rev_state = S9;
        else g_s_rev_state = S0;
    }
    break;
    case S9://只检测0,6
    {
        if (data == '0')
        {
            Uart_Comm.rev_ack_flag = UART_OK;
            // Uart_Comm.send_style = cst_AT_No_AT_MIPLDISCOVER; //直接跳转下一个状态机  AT_WAIT_MIPLDISCOVER
            g_s_rev_state = S0; //检测到结果,置空状态
        }
        else
        {
            g_s_rev_state = S0; //Bootstrap 流程处理成功 继续等待

        }
    }
    break;
    default:
    {
        g_s_rev_state = S0;
    }
    break;
    }
}

static void AT_MIPLOPEN_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {
    case S0:
    {
        if (data == 'E') g_s_rev_state = S1;
        else g_s_rev_state = S0;
    }
    break;

    case S1:
    {
        if (data == 'V') g_s_rev_state = S2;
        else g_s_rev_state = S0;
    }
    break;

    case S2:
    {
        if (data == 'E') g_s_rev_state = S3;
        else g_s_rev_state = S0;
    }
    break;

    case S3:
    {
        if (data == 'N') g_s_rev_state = S4;
        else g_s_rev_state = S0;
    }
    break;

    case S4:
    {
        if (data == 'T') g_s_rev_state = S5;
        else g_s_rev_state = S0;
    }
    break;
    case S5:
    {
        if (data == ':') g_s_rev_state = S6;
        else g_s_rev_state = S0;
    }
    break;

    case S6:
    {
        if (data == '0') g_s_rev_state = S7;
    }
    break;

    case S7:
    {
        if (data == ',') g_s_rev_state = S8;
        else g_s_rev_state = S0;
    }
    break;

    case S8://只检测0,6
    {
        if (data == '6')
        {
            Uart_Comm.rev_ack_flag = UART_OK;
            Uart_Comm.send_style = cst_AT_No_AT_MIPLOBSERVE; //直接跳转下一个状态机  AT_WAIT_MIPLDISCOVER
            g_s_rev_state = S0; //检测到结果,置空状态
        }
        else
        {
            g_s_rev_state = S0; //Bootstrap 流程处理成功 继续等待

        }
    }
    break;
    default:
    {
        g_s_rev_state = S0;
    }
    break;
    }
}

static void AT_WAIT_MIPLOBSERVE(u8 data) //+MIPLOBSERVE:0,18938,1,3202,0,-1
{
    static u8 wait_mipldiscover_state = S0;
//	static u8 point_num;
    switch (wait_mipldiscover_state)
    {
    case S0:
    {
        if (data == 'O') wait_mipldiscover_state = S1;
        else wait_mipldiscover_state = S0;
    }
    break;
    case S1:
    {
        if (data == 'B') wait_mipldiscover_state = S2;
        else wait_mipldiscover_state = S0;
    }
    break;
    case S2:
    {
        if (data == 'S') wait_mipldiscover_state = S3;
        else wait_mipldiscover_state = S0;
    }
    break;
    case S3:
    {
        if (data == 'E') wait_mipldiscover_state = S4;
        else wait_mipldiscover_state = S0;
    }
    break;
    case S4:
    {
        if (data == 'R') wait_mipldiscover_state = S5;
        else wait_mipldiscover_state = S0;
    }
    break;
    case S5:
    {
        if (data == 'V') wait_mipldiscover_state = S6;
        else wait_mipldiscover_state = S0;
    }
    break;
    case S6:
    {
        if (data == 'E')
        {
            memset(Uart_Comm.nbiot_discover_num,0,sizeof(Uart_Comm.nbiot_discover_num));
            Uart_Comm.nbiot_discover_num[0] = '0';
            Uart_Comm.rev_ack_flag = UART_OK;
            wait_mipldiscover_state = S0;
        }
        else
        {
            Uart_Comm.rev_ack_flag = UART_ERROR;
            wait_mipldiscover_state = S0; //检测到结果,置空状态
        }
    }
    break;
    default:
    {
        wait_mipldiscover_state = S0;
    }
    break;
    }
}

static void AT_NUWSTATS_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {
    case S0: //RSRP
    {
        if(data == 'S')
        {
            g_s_rev_state = S1;
        }
    }
    break;
    case S1: //RSRP
    {
        if(data == 'r')
        {
            g_s_rev_state = S2;
        }
    }
    break;
    case S2: //RSRP
    {
        if(data == ':')
        {
            s_g_temp1=0;
            memset(g_tmp_byte,0,sizeof(g_tmp_byte));
            g_s_rev_state = S3;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S3: //RSRP
    {
        if(data == '\r')
        {
            sscanf(g_tmp_byte,"%d",&s_g_rsrp_value);
            g_s_rev_state = S4;
        }
        else
        {
            if(((data >= '0') && (data <= '9')) || (data == '-'))
            {
                if(s_g_temp1 < 10)
                {
                    g_tmp_byte[s_g_temp1++] = data;// - 0x30;
                }
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
    }
    break;
    case S4: //Total power 跳过
    {
        if(data == ':')
        {
            g_s_rev_state = S5;
        }
    }
    break;
    case S5: //TX power 跳过
    {
        if(data == ':')
        {
            g_s_rev_state = S6;
        }
    }
    break;
    case S6: //+TX time: 跳过
    {
        if(data == ':')
        {
            g_s_rev_state = S7;
        }
    }
    break;
    case S7: //+RX time: 跳过
    {
        if(data == ':')
        {
            g_s_rev_state = S8;
        }
    }
    break;
    case S8: //+Cell ID:
    {
        if(data == 'I')
        {
            g_s_rev_state = S9;
        }
    }
    break;
    case S9: //+Cell ID:
    {
        if(data == 'D')
        {
            g_s_rev_state = S10;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S10: //+Cell ID:
    {
        if(data == ':')
        {
            s_g_temp1=0;
            memset(g_tmp_byte,0,sizeof(g_tmp_byte));
            g_s_rev_state = S11;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S11: //+Cell ID:
    {
        if(data == '\r')
        {
            memset(s_g_cell_id,0,sizeof(s_g_cell_id));
            sscanf(g_tmp_byte,"%s",s_g_cell_id);
            g_s_rev_state = S12;
        }
        else
        {
            if((data >= '0') && (data <= '9'))
            {
                if(s_g_temp1 < 10)
                {
                    g_tmp_byte[s_g_temp1++] = data;// - 0x30;
                }
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
    }
    break;
    case S12: //ECL:
    {
        if(data == 'L')
        {
            g_s_rev_state = S13;
        }
    }
    break;
    case S13: //ECL:
    {
        if(data == ':')
        {
            s_g_temp1=0;
            memset(g_tmp_byte,0,sizeof(g_tmp_byte));
            g_s_rev_state = S14;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S14://ECL:
    {
        if(data == '\r')
        {
            sscanf(g_tmp_byte,"%d",&s_g_ecl_value);
            g_s_rev_state = S15;
        }
        else
        {
            if((data >= '0') && (data <= '9'))
            {
                if(s_g_temp1 < 10)
                {
                    g_tmp_byte[s_g_temp1++] = data;
                }
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
    }
    break;

    case S15: //+SNR:
    {
        if(data == 'R')
        {
            s_g_temp1=0;
            g_s_rev_state = S16;
        }
    }
    break;
    case S16: //+SNR:
    {
        if(data == ':')
        {
            s_g_temp1=0;
            memset(g_tmp_byte,0,sizeof(g_tmp_byte));
            g_s_rev_state = S17;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S17://+SNR:
    {
        if(data == '\r')
        {
            sscanf(g_tmp_byte,"%d",&s_g_snr_value);
            g_s_rev_state = S18;
        }
        else
        {
            if(((data >= '0') && (data <= '9')) || (data == '-'))
            {
                if(s_g_temp1 < 10)
                {
                    g_tmp_byte[s_g_temp1++] = data;
                }
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
    }
    break;

    case S18: //+EARFCN:
    {
        if(data == 'N')
        {
            g_s_rev_state = S19;
        }
    }
    break;
    case S19: //+EARFCN:
    {
        if(data == ':')
        {
            s_g_temp1=0;
            memset(g_tmp_byte,0,sizeof(g_tmp_byte));
            g_s_rev_state = S20;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S20://+EARFCN:
    {
        if(data == '\r')
        {
            sscanf(g_tmp_byte,"%d",&s_g_earfcn_value);
            g_s_rev_state = S21;
        }
        else
        {
            if((data >= '0') && (data <= '9'))
            {
                if(s_g_temp1 < 10)
                {
                    g_tmp_byte[s_g_temp1++] = data;
                }
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
    }
    break;

    case S21: //+PCI:
    {
        if(data == 'I')
        {
            g_s_rev_state = S22;
        }
    }
    break;
    case S22: //+PCI:
    {
        if(data == ':')
        {
            s_g_temp1=0;
            memset(g_tmp_byte,0,sizeof(g_tmp_byte));
            g_s_rev_state = S23;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S23://+PCI:
    {
        if(data == '\r')
        {
            sscanf(g_tmp_byte,"%d",&s_g_pci_value);
            g_s_rev_state = S24;
        }
        else
        {
            if((data >= '0') && (data <= '9'))
            {
                if(s_g_temp1 < 10)
                {
                    g_tmp_byte[s_g_temp1++] = data;
                }
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
    }
    break;
    case S24://++RSRQ: 跳过
    {
        if (data == ':')
        {
            Uart_Comm.rev_ack_flag = UART_OK;
            g_s_rev_state = S0;
        }
    }
    break;
    default:
        break;
    }
}
/**
移动ACK应答:  +MIPLEVENT:0,26,7
电信ACK应答:  +MCWEVENT:9
*/
static u8 g_s_rev_ack_onenet_state=S0;
static void AT_NMGS_ACKID_MACHINE_ONENET(u8 data)
{
    switch (g_s_rev_ack_onenet_state)
    { 
        case S0:
        {
            if (data == ':')  g_s_rev_ack_onenet_state = S1;
            else g_s_rev_ack_onenet_state = S0;
        } break;     
 
        case S1:
        {
            if (data == '0')  g_s_rev_ack_onenet_state = S2;
            else g_s_rev_ack_onenet_state = S0;
        } break; 
 
        case S2:
        {
            if (data == ',')  g_s_rev_ack_onenet_state = S3;
            else g_s_rev_ack_onenet_state = S0;
        } break; 
        
         case S3:
        {
            if (data == '2')  g_s_rev_ack_onenet_state = S4;
            else g_s_rev_ack_onenet_state = S0;
        } break;   
        
         case S4:
        {
            if (data == '6')  g_s_rev_ack_onenet_state = S5;
            else g_s_rev_ack_onenet_state = S0;
        } break;          
         case S5:
        {
            if (data == ',')  g_s_rev_ack_onenet_state = S6;
            else g_s_rev_ack_onenet_state = S0;
        } break;           
         case S6:
        {
            if (data == '7')  
            {
				FrameSetMsgApp(MsgNbUplaod_AckOk);
                Uart_Comm.rev_ack_flag = UART_OK;
                g_s_rev_ack_onenet_state = S0; //检测到结果,置空状态   
            }
            else 
            {
                Uart_Comm.rev_ack_flag = UART_ERROR;
                g_s_rev_ack_onenet_state = S0; //检测到结果,置空状态
            }
        } break; 
         default:
		 {
			g_s_rev_ack_onenet_state = S0;
		 }break;
     }	
}
/**
移动ACK应答:  +MIPLEVENT:0,26,7
电信ACK应答:  +MCWEVENT:9
*/
static u8 g_s_rev_ack_coap_state=S0;
static void AT_NMGS_ACKID_MACHINE_COAP(u8 data)
{
    switch (g_s_rev_ack_coap_state)
    { 
        case S0:
        {
            if (data == 'E')  g_s_rev_ack_coap_state = S1;
            else g_s_rev_ack_coap_state = S0;
        } break;     
 
        case S1:
        {
            if (data == 'V')  g_s_rev_ack_coap_state = S2;
            else g_s_rev_ack_coap_state = S0;
        } break; 
 
        case S2:
        {
            if (data == 'E')  g_s_rev_ack_coap_state = S3;
            else g_s_rev_ack_coap_state = S0;
        } break; 
        
         case S3:
        {
            if (data == 'N')  g_s_rev_ack_coap_state = S4;
            else g_s_rev_ack_coap_state = S0;
        } break;   
        
         case S4:
        {
            if (data == 'T')  g_s_rev_ack_coap_state = S5;
            else g_s_rev_ack_coap_state = S0;
        } break;          
         case S5:
        {
            if (data == ':')  g_s_rev_ack_coap_state = S6;
            else g_s_rev_ack_coap_state = S0;
        } break;           
         case S6:
        {
            if (data == '9')//发送数据成功
            {
				FrameSetMsgApp(MsgNbUplaod_AckOk);
                Uart_Comm.rev_ack_flag = UART_OK;
                g_s_rev_ack_coap_state = S0; //检测到结果,置空状态   
            }
			else//其他事件回空闲,不返回OK或ERROR,放置影响后面判断 
            {
                g_s_rev_ack_coap_state = S0;
            }
        } break; 
         default:
		 {
			g_s_rev_ack_coap_state = S0;
		 }break;
     }	
}

static void AT_NMGS_ACKID_MACHINE(u8 data)
{
	AT_NMGS_ACKID_MACHINE_COAP(data);
	AT_NMGS_ACKID_MACHINE_ONENET(data);
}

u8 NbiotGetSocketCode(void)
{
	return tcp_socket_code;
}

void NbiotGetDiscover_num(char *discover_num)
{
    for(u8 i = 0; i < sizeof(Uart_Comm.nbiot_discover_num); i++)
    {
        *(discover_num+i) = Uart_Comm.nbiot_discover_num[i];
    }	
}

void NbiotGetWriteAck_num (char *ack_num)
{
    for(u8 i = 0; i < sizeof(Uart_Comm.nbiot_rev_serial_number); i++)
    {
        *(ack_num+i) = Uart_Comm.nbiot_rev_serial_number[i];
    }	
}

 u8 NbiotGetRevAck (void)
 {
	return Uart_Comm.nbiot_rev_ack;
 }
// 
// 
 u8 NbiotGetOnenetTypeDownOrder (void)
 {
	return Uart_Comm.Onenet_type_down_order;
 }
// 
 void ClearNbiotGetRevAck (void)
 {
	 Uart_Comm.nbiot_rev_ack = FALSE;
 }
void NbiotGetRSRP(s16 *rsrp_data)
{
    *rsrp_data = s_g_rsrp_value;
}

void NbiotGetSNR(s16 *snr_data)
{
    *snr_data = s_g_snr_value;
}

void NbiotGetCell_ID(u8 *cell_id_data)
{
	u8 i=0;
	u32 temp_cellid=0;
	u8 temp_cellidbuf[CELL_ID_SIZE];
	u8 temp_cellidbuf1[CELL_ID_SIZE];
	for(i=0;i<CELL_ID_SIZE;i++)
	{
	    if((s_g_cell_id[i]>=0x30)&&(s_g_cell_id[i]<=0x39))
		{
			temp_cellid += (s_g_cell_id[i]-0x30); 
			temp_cellid *=10;
		}
		temp_cellidbuf[i]=0;
		temp_cellidbuf1[i]=0;
	}
	temp_cellid /= 10;//去掉多余一个0
	for(i=0;i<CELL_ID_SIZE;i++)
	{
		if((temp_cellid&0x0F)<=9)
		{
			temp_cellidbuf[i] = '0'+(temp_cellid&0x0F);
		}
		else
		{
			temp_cellidbuf[i] = 'a'+(temp_cellid&0x0F)-10;
		}
		temp_cellid >>=4;
	}
	for(i=0;i<CELL_ID_SIZE;i++)
	{
		temp_cellidbuf1[9-i] = temp_cellidbuf[i];
	}
    for(i = 0; i < CELL_ID_SIZE; i++)
    {
        *(cell_id_data+i) = temp_cellidbuf1[i];
    }
}

void NbiotGetPCI(u16 *pci_data)
{
    *pci_data = s_g_pci_value;
}
void SetUartCommRevFlag (void)
{
	Uart_Comm.nbiot_rev_flag = TRUE; 
}

void NbiotGetEARFCN(u16 *earfcn_data)
{
    *earfcn_data = s_g_earfcn_value;
}

//udp开始查询数据
void UdpStartCheckData(void)
{
	udp_check_rev_data_flag = TRUE;
}
//udp结束查询数据
void UdpEndCheckData(void)
{
	udp_check_rev_data_flag = FALSE;
}
//udp是否需要读数据
u8 UdpIfNeedReadDataY7028(u8 connect_type)
{
	if(connect_type == NBIOT_CONNECT_UDP1)
	{
		return Uart_Comm.udp_rev_data_hint;
	}
	return Uart_Comm.udp_rev_data_hint;
	/*else
	{
		return Uart_Comm.tcp_rev_data_hint;
	}*/
}
u8 UdpIfNeedReadData(void)
{
	return (Uart_Comm.udp_rev_data_hint);
}
//清除udp/Tcp接收数据标志
void ClearUdpReadDataFlag(u8 connect_type)
{ 
	if(connect_type == NBIOT_CONNECT_UDP1)
	{
		Uart_Comm.udp_rev_data_hint = FALSE;
	}
	/*else
	{
		Uart_Comm.tcp_rev_data_hint = FALSE;
	}*/
}
//获取收到的数据类型
u8 GetRcvDataType(void)
{
	return Uart_Comm.nb_data_type;
}
void NbiotGetCSQ(u8 *csq_data)
{
    *csq_data = g_csq_value; 
}
//设置收到的数据类型
void SetRcvDataType(u8 temp_type)
{
	Uart_Comm.nb_data_type = temp_type;
}
u8 NbiotGetNBAND(void)
{
    return s_g_band_value;
}

#ifdef	__cplusplus
}
#endif

#endif

