/************************************************************************************
 * @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 MODULE_ML307A_TCP
#ifdef  __cplusplus
extern "C"
{
#endif
#include "nb_uart_driver_ml307a.h"
#include "nb_driver_ml307a.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_com; //接收状态机的状态变量
u8 udp_check_rev_data_flag = FALSE;

//信号强度和ICCID串号
u8 g_csq_value = 0x32;
u8 g_band_val = 0;
char 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;
u8 g_tmp_byte[11] = {0};
u16 s_g_earfcn_value = 0;//改成u16，因为接口返回的是U16指针
u32 s_g_earfcn_offset_value = 0;
u16 s_g_pci_value = 0;
char s_g_cell_id[CELL_ID_SIZE];
s16 s_g_rsrp_value = 0;//带符号可为负值
s16 s_g_rsrq_value = 0;//带符号可为负值
s16 s_g_rssi_value = 0;//带符号可为负值
s16 s_g_snr_value = 0;
u16 s_g_band_value = 0;
char s_g_tac[10];
s16 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;
/*****************************内部功能状态机***********************************/
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_CFUN_CK_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_SBAND_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_MUESTATS_MACHINE(u8 data);
static void AT_MIPCALL_MACHINE(u8 data);
/***************************************************************************
 * @fun_name     static void UART_Receive_IT(NBIOT_UART_HandleTypeDef *huart)
 * @brief        串口接收中断处理程序
 ***************************************************************************/
void NBUartReceiveIT(void)
{
    data = LPUart_ReceiveData(M0P_LPUART1);

    AT_NBIOT_REV_MACHINE(data);//tcp/udp 接收数据一直查询
    switch (Uart_Comm.send_style)
    {
    case cst_AT_No_AT:
    case cst_AT_No_AT_CFUN:
    case cst_AT_No_AT_CFUN0:
    case cst_AT_No_AT_CFUN1:
    case cst_AT_No_AT_TCPSEND:
    {
        AT_OK_AND_ERROR_MACHINE(data);
    }
    break;

    case cst_AT_No_AT_CIMI:
    {
        AT_CIMI_MACHINE(data);
    }
    break;
    case cst_AT_No_AT_CFUN_CK:
    {
        AT_CFUN_CK_MACHINE(data);
    }
    break;
    case cst_AT_No_AT_MIPCALL_how:
    {
        AT_MIPCALL_MACHINE(data);
    }
    break;
    case cst_AT_No_AT_MIPCALL:
    {
        AT_MIPCALL_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_SBAND:
    {
        AT_SBAND_MACHINE(data);
    }
    break;
    case cst_AT_No_AT_TCPSEND_ACK:
    {
        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_MUESTATS:
    {
        AT_MUESTATS_MACHINE(data);
    }
    break;
//    case cst_AT_No_AT_CCED:
//    {
//        AT_CCED_MACHINE(data);
//    }

    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;
        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;
}
/***************************************************************************
 * @fun_name    void NbiotRcv(u16 *len , u8 **data)
 * @brief       获取NBIOT数据
 * @param[out]  data: 数据存放地址  len:接收长度存放地址
 ***************************************************************************/
void NbiotRcv(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;
    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";
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;
    }
    }
}
//查询CFUN
/**
AT+CFUN?
+CFUN: 1

OK
*/
#define AT_CHECK_LEN_CFUN   8
static const u8 scAT_CFUN[AT_CHECK_LEN_CFUN] = "+CFUN: 1";
static void AT_CFUN_CK_MACHINE(u8 data)
{
    switch (g_s_rev_state_com)
    {
    case (AT_CHECK_LEN_CFUN-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_CFUN)
        {
            g_s_rev_state_com = ((scAT_CFUN[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 >= 0x30 && data <= 0x39)
        {
            rev_count = 0;
            buf_imsi[rev_count++] = data;
            g_s_rev_state = S1;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;

    case S1:
    {
        if (rev_count < 15)
        {
            buf_imsi[rev_count++] = data;
        }
        else
        {
            rev_count = 0;
            g_s_rev_state = S2;
        }
    }
    break;

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

    case S3:
    {
        if (data == 'K')
        {
            Uart_Comm.rev_ack_flag = UART_OK;
            g_s_rev_state = S0;
        }
        else
        {
            Uart_Comm.rev_ack_flag = UART_ERROR;
            g_s_rev_state = S0;
        }
    }
    break;
    default:
    {
        g_s_rev_state = S0;
        rev_count = 0;
        Uart_Comm.rev_ack_flag = UART_ERROR;
    }
    break;
    }
}

/**
+MIPURC: "recv",1,27
h \00!3x"G\04R     !?
*/
/**
+MIPURC: "rtcp",0,3,ABCDEF
+MIPURC: "rudp",1,3,78AB23
*/
#define AT_CHECK_LEN_MIPURC   11
static const u8 scAT_MIPURC[AT_CHECK_LEN_MIPURC] = "+MIPURC: \"r";
u16 ml307a_rev_data_len = 0;
#define REV_STATE_MIPURC_REV_TYPE   0x20 //判断数据来源
#define REV_STATE_MIPURC_REV_STATE1    0x21

#define REV_STATE_MIPURC_REV_STATE2    0x22
#define REV_STATE_MIPURC_REV_STATE3    0x23
#define REV_STATE_MIPURC_REV_STATE4    0x24
#define REV_STATE_MIPURC_REV_STATE5    0x25
#define REV_STATE_MIPURC_REV_STATE6    0x26
#define REV_STATE_MIPURC_REV_LEN    0x27 //接收数据长度
#define REV_STATE_MIPURC_REV_DATA   0x28 //接收数据
static void AT_NBIOT_REV_MACHINE(u8 data)
{
    switch (g_s_rev_state_coap)
    {
    case (AT_CHECK_LEN_MIPURC-1):
    {
        g_s_rev_state_coap = ('r' == data ? (REV_STATE_MIPURC_REV_STATE1) : 0);
        break;
    }
		 case (REV_STATE_MIPURC_REV_STATE1):
    {
			g_s_rev_state_coap = (('u' == data ||'t' == data)? (REV_STATE_MIPURC_REV_STATE2) : 0);
        break;
		}
				 case (REV_STATE_MIPURC_REV_STATE2):
    {
			g_s_rev_state_coap = (('d' == data ||'c' == data)? (REV_STATE_MIPURC_REV_STATE3) : 0);
        break;
		}
		case (REV_STATE_MIPURC_REV_STATE3):
    {
			g_s_rev_state_coap = ('p' == data ? (REV_STATE_MIPURC_REV_STATE4) : 0);
        break;
		}
					case (REV_STATE_MIPURC_REV_STATE4):
    {
			g_s_rev_state_coap = ('\"' == data ? (REV_STATE_MIPURC_REV_STATE5) : 0);
        break;
		}
			case (REV_STATE_MIPURC_REV_STATE5):
    {
			g_s_rev_state_coap = (',' == data ? (REV_STATE_MIPURC_REV_TYPE) : 0);
        break;
		}
		
    case (REV_STATE_MIPURC_REV_TYPE):
    {
        ml307a_rev_data_len = 0;
        Uart_Comm.rev_len = 0;
        if ('1' == data)
        {
            Uart_Comm.nb_data_type = NB_RCV_COAP_DATA;
            g_s_rev_state_coap = REV_STATE_MIPURC_REV_STATE6;
        }
        else if ('2' == data)
        {
            Uart_Comm.nb_data_type = NB_RCV_UDP1_DATA;
            g_s_rev_state_coap = REV_STATE_MIPURC_REV_STATE6;
        }
        else if ('3' == data)
        {
            Uart_Comm.nb_data_type = NB_RCV_UDP2_DATA;
            g_s_rev_state_coap = REV_STATE_MIPURC_REV_STATE6;
        }
        else
        {
            g_s_rev_state_coap = 0;
        }
        break;
    }
    case (REV_STATE_MIPURC_REV_STATE6):
    {
        g_s_rev_state_coap = (',' == data ? (REV_STATE_MIPURC_REV_LEN) : 0);
        break;
    }
    case (REV_STATE_MIPURC_REV_LEN):
    {
        if ((data >= '0') && (data <= '9'))
        {
            ml307a_rev_data_len *= 10;
            ml307a_rev_data_len += (data - 0x30);
        }
        else if (',' == data)
        {
            if (ml307a_rev_data_len < ML307A_UP_BUF_SIZE)
            {
                g_s_rev_state_coap = REV_STATE_MIPURC_REV_DATA;
            }
            else
            {
                g_s_rev_state_coap = 0;
            }
        }
        break;
    }
    case (REV_STATE_MIPURC_REV_DATA):
    {
        if (Uart_Comm.rev_len < ml307a_rev_data_len * 2)
        {
            Uart_Comm.rev_buf[Uart_Comm.rev_len++] = data;
        }
        else
        {
            Uart_Comm.nbiot_rev_flag = TRUE;
            g_s_rev_state_coap = 0;
        }
        break;
    }
    default:
    {
        if (g_s_rev_state_coap < AT_CHECK_LEN_MIPURC)
        {
            g_s_rev_state_coap = (scAT_MIPURC[g_s_rev_state_coap] == data ? (g_s_rev_state_coap + 1) : 0);
        }
        else
        {
            g_s_rev_state_coap = 0;
        }

    }
    }
}

/**
AT+MIPCALL?
+MIPCALL: 1,1,"10.63.51.172"
*/
#define AT_CHECK_LEN_MIPCALL   15
static const u8 scAT_MIPCALL[AT_CHECK_LEN_MIPCALL] = "+MIPCALL: 1,1,\"";
static void AT_MIPCALL_MACHINE(u8 data)
{
    switch (g_s_rev_state_com)
    {
    case (AT_CHECK_LEN_MIPCALL - 1):
    {
        if (data != '0')
        {
            Uart_Comm.rev_ack_flag = UART_OK;
            g_s_rev_state_com = 0;
        }
        else
        {
            Uart_Comm.rev_ack_flag = UART_ERROR;
            g_s_rev_state_com = 0;
        }

        break;
    }

    default:
    {
        if (g_s_rev_state_com < AT_CHECK_LEN_MIPCALL)
        {
            g_s_rev_state_com = (scAT_MIPCALL[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 = (g_csq_value == 99 ? UART_ERROR: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;
    }
    }
}
/**AT+MUESTATS="sband"
+MUESTATS: "sband",8
OK
*/
#define AT_CHECK_LEN_SBAND     19
static const u8 scAT_SBAND[AT_CHECK_LEN_SBAND] = "+MUESTATS: \"sband\",";
static void AT_SBAND_MACHINE(u8 data)
{
	switch (g_s_rev_state_com)
    {
		case (AT_CHECK_LEN_SBAND - 1):
		{
	        if (data != 'O')
        {
            if (data >= '0' && data <= '9') //数字
            {
                s_g_band_value *= 10;
                s_g_band_value += (data - 0x30);
            }
        }
        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_SBAND)
        {
            g_s_rev_state_com = (scAT_SBAND[g_s_rev_state_com] == data ? (g_s_rev_state_com + 1) : 0);
            s_g_band_value = ('d' == data ? (0) : s_g_band_value);
        }
        else
        {
            g_s_rev_state_com = 0;
        }
        break;
		}
		}
	
	
}


/**AT+MUESTATS="cell"
+MUESTATS: "scell",4,460,00,1300,,293,-990,-195,-520,-90,

OK
*/
#define AT_CHECK_LEN_MUESTATS   19
#define AT_MUESTATS_EARFCN      0x20
#define AT_MUESTATS_PCI         0x21
#define AT_MUESTATS_RSRP        0x22
#define AT_MUESTATS_SNR         0x23
static const u8 scAT_MUESTATS[AT_CHECK_LEN_MUESTATS] = "+MUESTATS: \"scell\",";
static void AT_MUESTATS_MACHINE(u8 data)
{
    static u8 count = 0;
    switch (g_s_rev_state_com)
    {
    case (AT_CHECK_LEN_MUESTATS - 1):
    {
        if (data == ',')
        {
            count++;
        }
        if (count == 4)
        {
            g_s_rev_state_com = AT_MUESTATS_EARFCN;
            s_g_earfcn_value = 0;
        }
        else if (count == 6)
        {
            g_s_rev_state_com = AT_MUESTATS_PCI;
            s_g_pci_value = 0;
        }
        else if (count == 7)
        {
            g_s_rev_state_com = AT_MUESTATS_RSRP;
            s_g_rsrp_value = 0;
        }
        else if (count == 10)
        {
            g_s_rev_state_com = AT_MUESTATS_SNR;
            s_g_snr_value = 0;
        }
        break;
    }
    case AT_MUESTATS_EARFCN:
    {
        if (data == ',') //频点
        {
            int tmp1 = 0;
            count++;
            sscanf(g_ecl_snr_value, "%d", &tmp1);
            s_g_earfcn_value = tmp1;
            s_g_temp1 = 0;
            memset(g_ecl_snr_value, 0, sizeof(g_ecl_snr_value));
            g_s_rev_state_com = AT_CHECK_LEN_MUESTATS - 1;
        }
        else
        {
            if (s_g_temp1 < 10)
            {
                g_ecl_snr_value[s_g_temp1++] = data;
            }
            else
            {
                g_s_rev_state_com = 0;
            }
        }

        break;
    }
    case AT_MUESTATS_PCI:
    {
        if (data == ',')//PCI
        {
            int tmp = 0;
            count++;
            sscanf(g_ecl_snr_value, "%d", &tmp);
            s_g_pci_value = tmp;
            s_g_temp1 = 0;
            memset(g_ecl_snr_value, 0, sizeof(g_ecl_snr_value));
            g_s_rev_state_com = AT_CHECK_LEN_MUESTATS - 1;
        }
        else
        {
            if (s_g_temp1 < 5)
            {
                g_ecl_snr_value[s_g_temp1++] = data;
            }
            else
            {
                g_s_rev_state_com = 0;
            }
        }

        break;
    }
    case AT_MUESTATS_RSRP:
    {
        if (data == ',')//RSRP
        {
            int tmp = 0;
            count++;
            sscanf(g_ecl_snr_value, "%d", &tmp);
            s_g_rsrp_value = tmp;
            s_g_temp1 = 0;
            memset(g_ecl_snr_value, 0, sizeof(g_ecl_snr_value));
            g_s_rev_state_com = AT_CHECK_LEN_MUESTATS - 1;
        }
        else
        {
            if (((data >= '0') && (data <= '9')) || (data == '-'))
            {
                if (s_g_temp1 < 10)
                {
                    g_ecl_snr_value[s_g_temp1++] = data;// - 0x30;
                }
            }
            else
            {
                g_s_rev_state_com = 0;
            }
        }


        break;
    }
    case AT_MUESTATS_SNR:
    {
        if (data == ',')//SNR
        {
            int tmp = 0;
            count++;
            sscanf(g_ecl_snr_value, "%d", &tmp);
            s_g_snr_value = tmp;
            s_g_temp1 = 0;
            memset(g_ecl_snr_value, 0, sizeof(g_ecl_snr_value));
            g_s_rev_state_com = 0;
            Uart_Comm.rev_ack_flag = UART_OK;
        }
        else
        {
            if (((data >= '0') && (data <= '9')) || (data == '-'))
            {
                if (s_g_temp1 < 10)
                {
                    g_ecl_snr_value[s_g_temp1++] = data;
                }
            }
            else
            {
                g_s_rev_state_com = 0;
            }
        }


        break;
    }



    default:
    {
        if (g_s_rev_state_com < AT_CHECK_LEN_MUESTATS)
        {
            g_s_rev_state_com = (scAT_MUESTATS[g_s_rev_state_com] == data ? (g_s_rev_state_com + 1) : 0);
            count = 0;
        }
        else
        {
            g_s_rev_state_com = 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 == 'C')
        {
            g_s_rev_state = S1;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;

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


    case S2:
    {
        if (data == 'S')
        {
            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 == ':')
        {
            g_s_rev_state = S5;
        }
        else
        {
            g_s_rev_state = S0;
        }
    }
    break;
    case S5:
    {
        if (data == 0x20) //空格
        {
            rev_count = 0;
            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"
*/
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>=0x23)
			{
				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;
    }
}
/**
AT+MCCID
+MCCID: 898604B21521D0139777

OK
*/
#define AT_CHECK_LEN_ICCID   8
static const u8 scAT_ICCID[AT_CHECK_LEN_ICCID] = "+MCCID: ";
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;
    }
    }
}
/**
AT+CPIN?
+CPIN: READY
*/
#define AT_CHECK_LEN_CPIN   12
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_MIPOPEN[AT_CHECK_LEN_MIPOPEN] = "+MIPOPEN: ";
//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
#define AT_SOCKET_STATE  0x20
#define AT_CONNECTED_STATE1 0x21
#define AT_CONNECTED_STATE2 0x22
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;
        if (data == ' ')
        {
            g_s_rev_state_com = AT_SOCKET_STATE;
        }
        else
        {
            g_s_rev_state_com = 0;
        }
        break;
    }
    case AT_SOCKET_STATE:
    {
        if ((data >= '0') && (data <= '5'))
        {
            tcp_socket_code = data;
            g_s_rev_state_com = AT_CONNECTED_STATE1;
        }
        else
        {
            g_s_rev_state_com = 0;
        }
        break;
    }
    case AT_CONNECTED_STATE1:
    {
        g_s_rev_state_com = ((data == ',') ?  AT_CONNECTED_STATE2 : 0);
        break;
    }
    case AT_CONNECTED_STATE2:
    {
        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_MIPOPEN)
        {
            g_s_rev_state_com = (((scAT_MIPOPEN[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;
//    }
//    }
//}
/*
AT+MIPSEND=1,5,"1213141516"
+MIPSEND: 1,5

OK

+MIPURC: "ack",1,5
*/
#define AT_CHECK_MIPSEND_ACK   14
static const u8 scAT_MIPSEND[AT_CHECK_MIPSEND_ACK] = "+MIPURC: \"ack\"";
static void AT_MIPSEND_MACHINE(u8 data)
{
    switch (g_s_rev_state_com)
    {
    case (AT_CHECK_MIPSEND_ACK-1):
    {
        Uart_Comm.rev_ack_flag =  UART_OK ;
        g_s_rev_state_com = 0;
		FrameSetMsgApp(MsgNbUplaod_AckOk);
        break;
    }
    default:
    {
        if (g_s_rev_state_com < AT_CHECK_MIPSEND_ACK)
        {
            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   11
static const u8 scAT_MIPCLOSE[AT_CHECK_LEN_MIPCLOSE] = "+MIPCLOSE: ";
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 == ' ') ? 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;
//    }
//    }
//}


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 UdpIfNeedReadData(void)
{
    return Uart_Comm.udp_rev_data_hint;
}
//清楚udp接收数据标志
void ClearUdpReadDataFlag(void)
{
    Uart_Comm.udp_rev_data_hint = FALSE;
}
//获取收到的数据类型
u8 GetRcvDataType(void)
{
    return Uart_Comm.nb_data_type;
}
void NbiotGetCSQ(u8 *csq_data)
{
    *csq_data = g_csq_value;
}

u8 NbiotGetNBAND(void)
{
    return s_g_band_value;
}

#ifdef  __cplusplus
}
#endif

#endif

