/************************************************************************************
 * @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_BC28
#ifdef	__cplusplus
extern "C"
{
#endif
#include "nb_uart_driver_bc28.h"
#include "nb_driver_bc28.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 udp_check_rev_data_flag = FALSE;
u16 udp_rev_data_len=0;

//信号强度和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 g_snr_value[4] = {0};
//u8 IMEI_num[8] = {0};
//u8 IMSI_num[8] = {0};
//u8 ICCID_num[10] = {0};
u8 s_g_buf[10] = {0};
u8 s_g_temp1 = 0;

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;
u8 bc28_regind_flag=0;
u8 g_s_rev_state_com=0;
/*****************************内部功能状态机***********************************/
static void AT_OK_AND_ERROR_MACHINE(u8 data);
static void AT_CGATT_HOW_MACHINE(u8 data);
static void AT_CEREG_MACHINE(u8 data);
//static void AT_NCDP_CK_MACHINE(u8 data);
static void AT_NCDP_MACHINE(u8 data);
static void AT_CIMI_MACHINE(u8 data);
static void AT_NBIOT_REV_MACHINE(u8 data);

//static void AT_CGSN_MACHINE(u8 data);
static void AT_NUWSTATS_MACHINE(u8 data);
static void AT_CSQ_MACHINE(u8 data);
static void AT_IMEI_MACHINE(u8 data);
//static void AT_CHECK_ERROR_NUM_MACHINE(u8 data);
static void AT_GET_CCLK_MACHINE(u8 data);
static void AT_ICCID_MACHINE(u8 data);
static void AT_QIOPEN_MACHINE(u8 data);
static void AT_UDP_REV_MACHINE(u8 data);
//static void AT_UDP_READ_MACHINE(u8 data);
static void AT_MIPLOPEN_MACHINE(u8 data);
static void AT_WAIT_MIPLDISCOVER(u8 data);
//static void AT_MIPLCLOSE_MACHINE(u8 data);
static void AT_CM_NBIOT_REV_MACHINE(u8 data);
static void AT_MIPLCLOSE_MACHINE(u8 data);
//static void AT_QPOWD_MACHINE(u8 data);
static void AT_NMGS_ACKID_MACHINE(u8 data);
static void AT_MIPLNOTIFY_ACKID_MACHINE(u8 data);
static void AT_NSOCR_MACHINE(u8 data);
/***************************************************************************
 * @fun_name     static void UART_Receive_IT(NBIOT_UART_HandleTypeDef *huart)
 * @brief        串口接收中断处理程序
 ***************************************************************************/
void NBUartReceiveIT(void)
{
	data = LPUart_ReceiveData(M0P_LPUART1);

//    if(TASK_IDLE == NbiotIfIdle())
//    {
//		if(udp_check_rev_data_flag)
//		{
//			//Uart_Comm.send_style = cst_AT_No_AT_UDP_REV;
//		}
//	}
	AT_UDP_REV_MACHINE(data);   //UDP接收数据一直查询
	
	AT_NBIOT_REV_MACHINE(data);//coap接收数据一直查询
	AT_CM_NBIOT_REV_MACHINE(data);//+MIPLWRITE:
	AT_NCDP_MACHINE(data);//自动注册模式下在主循环检测是否注册成功
    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_NBAND:
        case cst_AT_No_AT_CGATT:
        case cst_AT_No_AT_CGDCONT:
        //case cst_AT_No_AT_NSOCR:
        case cst_AT_No_AT_NSOST:
        case cst_AT_No_AT_NSOCL:
        case cst_AT_No_AT_NETCLOSE:
        case cst_AT_No_AT_COPS:
        case cst_AT_No_AT_NSMI:
        case cst_AT_No_AT_NNMI:
        case cst_AT_No_AT_NMGS:
        case cst_AT_No_AT_CPSMS:
        case cst_AT_No_AT_NCSEARFCN:
        case cst_AT_No_AT_NPTWEDRXS:
        case cst_AT_No_AT_CFUN0:
        case cst_AT_No_AT_CFUN1:
		//case cst_AT_No_AT_QPOWD:
        case cst_AT_No_AT_MIPLCONFIG:		
		case cst_AT_No_AT_MIPLCONFIG_CK:
        case cst_AT_No_AT_MIPLCREATE:		
        case cst_AT_No_AT_MIPLADDOBJ:
        case cst_AT_No_AT_MIPLDISCOVERRSP:
		case cst_AT_No_AT_MIPLNOTIFY:
		case cst_AT_No_AT_MIPLWRITE:
		case cst_AT_No_AT_MIPLDELOBJ:
		case cst_AT_No_AT_MIPLDEL:
		//case cst_AT_No_AT_NCDP_CK://BC28
		case cst_AT_No_AT_QPOWD:
		case cst_AT_QSCLK_CLOSE:
        case cst_AT_No_AT_NSONMI: 
        {
            AT_OK_AND_ERROR_MACHINE(data); 
        }
        break;

        /*case cst_AT_No_AT_NCDP_CK:
        {
            AT_NCDP_CK_MACHINE(data);
        }
        break;*/

        /*case cst_AT_No_AT_NCDP: //新增20170706
        {
            AT_NCDP_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_CEREG:
        {
            AT_CEREG_MACHINE(data);
        }
        break;
        /*case cst_AT_No_AT_NBIOT_REV:
        {
            AT_NBIOT_REV_MACHINE(data);
        }
        break;*/

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

        //        case cst_AT_No_AT_CGSN:
        //        {
        //            AT_CGSN_MACHINE(data);
        //        } break;

        case cst_AT_No_AT_NUESTATS:
        {
            AT_NUWSTATS_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_QIOPEN:
        {
            AT_QIOPEN_MACHINE(data);
        }
        break;		
//		case cst_AT_No_AT_UDP_REV:
//		{
//			AT_UDP_REV_MACHINE(data);
//		}
//		break;
//		case cst_AT_No_AT_UPD_READ:
//		{
//			AT_UDP_READ_MACHINE(data);
//		}
//		break;
        case cst_AT_No_AT_MIPLOPEN:
        {
            AT_MIPLOPEN_MACHINE(data);
        }
        break;
        case cst_AT_No_AT_MIPLDISCOVER:
        {
            AT_WAIT_MIPLDISCOVER(data);
        }
		break;
		case cst_AT_No_AT_MIPLCLOSE:                    
        {
            AT_MIPLCLOSE_MACHINE(data); //////////////////
        }
        break;
		/*case cst_AT_No_AT_QPOWD:                    
        {
            AT_QPOWD_MACHINE(data); 
        }
        break;*/
		case cst_AT_No_AT_NMGS_ACKID:                    
        {
            AT_NMGS_ACKID_MACHINE(data); 
        }
        break;
		case cst_AT_No_AT_MIPLNOTIFY_ACKID:                    
        {
            AT_MIPLNOTIFY_ACKID_MACHINE(data); 
        }
        break;
		
		case cst_AT_No_AT_NSOCR:
		{
		    AT_NSOCR_MACHINE(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;
        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 NbiotRcvTransTwo(u16 *len, u8 **data)
{
    u16 i;
    u8 temp_data;
    for (i = 0; i < (Uart_Comm.rev_len / 4); i++)
    {
        if (Uart_Comm.rev_buf_handled[i * 2] > 0x5A)//大于‘Z’，则为小写字母，‘a’的ASCII码值为97，则应减87
        {
            temp_data = (Uart_Comm.rev_buf_handled[i * 2] - 0x57) << 4;
        }
        else if (Uart_Comm.rev_buf_handled[i * 2] > 0x39)
        {
            temp_data = (Uart_Comm.rev_buf_handled[i * 2] - 0x37) << 4;
        }
        else
        {
            temp_data = (Uart_Comm.rev_buf_handled[i * 2] - 0x30) << 4;
        }

        if (Uart_Comm.rev_buf_handled[i * 2 + 1] > 0x5A)
        {
            temp_data |= (Uart_Comm.rev_buf_handled[i * 2 + 1] - 0x57);
        }
        else if (Uart_Comm.rev_buf_handled[i * 2 + 1] > 0x39)
        {
            temp_data |= (Uart_Comm.rev_buf_handled[i * 2 + 1]) - 0x37;
        }
        else
        {
            temp_data |= (Uart_Comm.rev_buf_handled[i * 2 + 1]) - 0x30;
        }
        Uart_Comm.rev_buf_handled[i] = temp_data;
    }
    *data = Uart_Comm.rev_buf_handled;
    *len = Uart_Comm.rev_len / 4;
}
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))
		{
			NbiotRcvTransOne(len, data);
		}
		else
		{
			if(Nbiot_Para.Telecom == SIM_CARD_MOBILE)//移动卡
			{
				NbiotRcvTransOne(len, data);
				NbiotRcvTransTwo(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 = 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;
                }
                if (cst_AT_No_AT_NSOST == Uart_Comm.send_style)
                {
                    g_s_rev_state = S0;
                }
                else
                {
                    g_s_rev_state = S_IDLE; //检测到结果,置空状态
                }
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

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

//查询CGATT
static void AT_CGATT_HOW_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {
        case S0:
        {
            if (data == '+')
            {
                g_s_rev_state = S1;
            }
        }
        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 == '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 == 'T')
            {
                g_s_rev_state = S6;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S6:
        {
            if (data == ':')
            {
                g_s_rev_state = S8;//BC28跳过空格
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

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

        case S8:
        {
			sys_set_net_state(2, (data-'0'));
            if (data == '1')
            {
                Uart_Comm.rev_ack_flag = UART_OK;
                g_s_rev_state = S_IDLE; //检测到结果,置空状态
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

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

//查询CEREG
static void AT_CEREG_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {
        case S0:
        {
            if (data == '+')
            {
                g_s_rev_state = S1;
            }
        }
        break;

        case S1:
        {
            if (data == 'C')
            {
                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 == 'R')
            {
                g_s_rev_state = S4;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

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

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

        case S6:
        {
            if (data == ':')
            {
                g_s_rev_state = S8;//BC28跳过空格
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

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

        case S8:
        {
            //0,1,2 or 3 and command successful:
            if (data >= '0' && data <= '3')
            {
                g_s_rev_state = S9;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

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

        case S10:
        { 
			sys_set_net_state(1, (data-'0'));
            if (data == '1' || data == '5')
            {
                Uart_Comm.rev_ack_flag = UART_OK;
                g_s_rev_state = S_IDLE; //检测到结果,置空状态
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

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

//static void AT_NCDP_CK_MACHINE(u8 data)
//{
//    switch (g_s_rev_state)
//    {
//        case S0:
//        {
//            if(data == ':')
//            {
//                g_s_rev_state = S1;
//            }
//        }
//        break;

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

//        case S2:
//        {
//            if(data == 'R')
//            {
//                g_s_rev_state = S3;
//            }
//            else
//            {
//                Uart_Comm.rev_ack_flag = UART_ERROR;
//                g_s_rev_state = S0;
//            }
//        }
//        break;

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

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

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

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

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

//        case S8:
//        {
//            if(data == 'E')
//            {
//                g_s_rev_state = S9;
//            }
//            else
//            {
//                g_s_rev_state = S0;
//            }
//        }
//        break;

//        case S9:
//        {
//            if(data == 'R')
//            {
//////            Uart_Comm.rev_ack_flag = UART_OK;
//                g_s_rev_state = S10;
//            }
//            else
//            {
//                g_s_rev_state = S0;
//            }
//        }
//        break;

//        case S10:
//        {
//            if(data == 'E')
//            {
//////            Uart_Comm.rev_ack_flag = UART_OK;
//                g_s_rev_state = S11;
//            }
//            else
//            {
//                g_s_rev_state = S0; //检测到结果,置空状态
//            }
//        }
//        break;

//        case S11:
//        {
//            if(data == 'D')
//            {
//                g_s_rev_state = S12;
//            }
//            else
//            {
//                g_s_rev_state = S0;
//            }
//        }
//        break;

//        case S12:
//        {
//            if(data == 'K')
//            {
//                Uart_Comm.rev_ack_flag = UART_OK;
//                g_s_rev_state = S0;
//            }
//        }
//        break;

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

u8 g_s_rev_state_ncdp=S0;
static void AT_NCDP_MACHINE(u8 data)
{
    switch (g_s_rev_state_ncdp)
    {
        case S0:
        {
            if(data=='T')
            {
                g_s_rev_state_ncdp=S1;
            }
        }
        break;

        case S1:
        {
            if(data=='I')
            {
                g_s_rev_state_ncdp=S2;
            }
        }
        break;

        case S2:
        {
            if(data=='N')
            {
                g_s_rev_state_ncdp=S3;
            }
        }
        break;

        case S3:
        {
            if(data=='D')
            {
                g_s_rev_state_ncdp=S4;
            }
        }
        break;

        case S4:
        {
            if(data==':')
            {
                //g_s_rev_state_ncdp=S5;//BC25
				g_s_rev_state_ncdp=S6;//BC28
            }
        }
        break;

        case S5:
        {
            if(data==' ')
            {
                g_s_rev_state_ncdp=S6;
            }
        }
        break;

        case S6:
        {
            if(data=='3')
            {
                Uart_Comm.rev_ack_flag = UART_OK;
				bc28_regind_flag = TRUE;
            }
            g_s_rev_state_ncdp=S0;
        }
        break;

        default:
        {
            g_s_rev_state_ncdp = S0;
        }
        break;
    }
}
u8 getRegindState(void)
{
	return bc28_regind_flag;
}
void clearRegindState(void)
{
	bc28_regind_flag = FALSE;
}
//获取返回的SOCKET
/*AT+NSOCR=DGRAM,17,12341
0

OK*/
u8 temp_socket='0';
static void AT_NSOCR_MACHINE(u8 data)
{
	switch (g_s_rev_state)
	{
		case S0:
		{
			if (data == '\r')
			{
				g_s_rev_state = S1;
			}
		}
		break;
		case S1:
		{
			if (data == '\n')
			{
				temp_socket ='0';
				g_s_rev_state = S2;
			}
			else
			{
				g_s_rev_state = S0;
			}
		}
		break;

		case S2:
		{
			if (data == '\r')
			{
				g_s_rev_state = S3;
			}
			else
			{
				if (data >= '0' && data <= '9')
				{
					temp_socket = data;
					Uart_Comm.rev_ack_flag = UART_OK;
					g_s_rev_state = S0;
				}
				else if (data == 'E')//error
				{
					Uart_Comm.rev_ack_flag = UART_ERROR;
					g_s_rev_state = S0;
				}
			}        
		}
		break;
		case S3:
		{
			Uart_Comm.rev_ack_flag = UART_ERROR;
			g_s_rev_state = S0;
		}
		break;
		default:
		break;
	}
}

u8 getTempUdpCode(void)
{
	return temp_socket;
}

static void AT_QIOPEN_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    {
        case S0:
        {
            if(data=='+')
            {
                g_s_rev_state=S1;
            }
        }
        break;
        case S1:
        {
            if(data=='Q')
            {
                g_s_rev_state=S2;
            }
        }
        break;
        case S2:
        {
            if(data=='I')
            {
                g_s_rev_state=S3;
            }
        }
        break;
        case S3:
        {
            if(data=='O')
            {
                g_s_rev_state=S4;
            }
        }
        break;
        case S4:
        {
            if(data=='P')
            {
                g_s_rev_state=S5;
            }
        }
        break;
        case S5:
        {
            if(data=='E')
            {
                g_s_rev_state=S6;
            }
        }
        break;	
        case S6:
        {
            if(data=='N')
            {
                g_s_rev_state=S7;
            }
        }
        break;			
        case S7:
        {
            if(data==':')
            {
                Uart_Comm.rev_ack_flag = UART_OK;
            }
            g_s_rev_state=S0;
        }
        break;
        default:
        {
            g_s_rev_state = S0;
        }
        break;
    }
}
//+QIURC: "recv",0    +NSONMI:0,32
u8 g_s_udprev_state= S0;
static void AT_UDP_REV_MACHINE(u8 data)
{
    switch (g_s_udprev_state)
    {
        case S0:
        {
            if(data=='+')
            {
                g_s_udprev_state=S1;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
        case S1:
        {
            if(data=='N')
            {
                g_s_udprev_state=S2;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
        case S2:
        {
            if(data=='S')
            {
                g_s_udprev_state=S3;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
        case S3:
        {
            if(data=='O')
            {
                g_s_udprev_state=S4;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
        case S4:
        {
            if(data=='N')
            {
                g_s_udprev_state=S5;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
        case S5:
        {
            if(data=='M')
            {
                g_s_udprev_state=S6;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;			
		case S6:
        {
            if(data=='I')
            {
                g_s_udprev_state=S7;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
        case S7:
        {
            if(data==':')
            {
                g_s_udprev_state=S8;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
		case S8:
        {    	
            Uart_Comm.rev_len=0;
            udp_rev_data_len = 0;
            if ('0' == data)
            {   
                Uart_Comm.nb_data_type = NB_RCV_UDP1_DATA;
                g_s_udprev_state=S9;
            }
            else if ('1' == data)
            {   
                Uart_Comm.nb_data_type = NB_RCV_UDP2_DATA;
                g_s_udprev_state=S9;
            }
            else
            {
                g_s_udprev_state = S0;
            }
        }
        break;
		case S9:
        {
            if(data==',')
            {
                g_s_udprev_state=S10;
            }
			else
			{
				g_s_udprev_state=S0;
			}
        }
        break;
        case S10:
        {  
            if (data != ',')
            {
				 if ((data >= '0') && (data <= '9'))
                {
					udp_rev_data_len *= 10;
					udp_rev_data_len += (data -0x30);
                }
            }
            else
            {
				if(udp_rev_data_len < BC25_UP_BUF_SIZE * 2)
				{
					g_s_udprev_state = S11;
				}
				else
				{
					g_s_udprev_state = 0;
				}
            }
        }
        break;
        case S11:
        {
			if (Uart_Comm.rev_len < udp_rev_data_len * 2)
			{   
				Uart_Comm.rev_buf[Uart_Comm.rev_len++] = data;
			}
			else
			{
//				if(0 < Uart_Comm.rev_len) //沈阳规约修改，UDP接到数据再查一次，应答帧和下发指令间隔太短，不查会丢指令
//				{
//					udp_rev_data_check = TRUE ;
//				}
				Uart_Comm.rev_ack_flag = UART_OK;
				Uart_Comm.nbiot_rev_flag = TRUE; //先做写应答然后处理
				//Uart_Comm.nb_data_type = NB_RCV_UDP_DATA;
                g_s_udprev_state = S0;
			}
        }
        break; 
        
        default:
        {
            g_s_udprev_state = S0;
        }
        break;
    }	
}


u8  udp_rev_data_check = FALSE ;
u8 g_s_rev_state_udp=S0;
void UdpReadStart(void)
{
	g_s_rev_state_udp = S0;
}
void UdpReadStop(void)
{
	g_s_rev_state_udp = S_IDLE;
}

//沈阳规约修改，UDP接到数据再查一次，应答帧和下发指令间隔太短，不查会丢指令
u8 Check_Udp_Data_Received()
{
	return udp_rev_data_check;
}
void Clear_Udp_Rec_Data_Flag(void)
{
	udp_rev_data_check = FALSE;
}

/*
MCC     MNC        operator
460     00         中国移动
460     01         中国联通
460     02         中国移动
460     03         中国电信
460     05         中国电信
460     06         中国联通
460     07         中国移动
460     20         中国铁通
 */
//查询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'))
                    {
                        Nbiot_Para.imsi[rev_count++] = data;
                    }
                }
                else
                {
                    rev_count = 0;
                    Nbiot_Para.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;
    }
}
static void AT_NBIOT_REV_MACHINE(u8 data)
{
    static u8 rev_count = 0;
    static u16 rev_len = 0; //记录接收数据长度
    static u8 gprs_rev_len[4];
    switch (g_s_rev_state_coap)
    {
		case S0:
		{
			//等待接收GPRS数据
			if (data == '+')
			{
				g_s_rev_state_coap = S1;
			}
		}
		break;
		case S1:
		{
			if (data == 'N')
			{
				g_s_rev_state_coap = S2;
			}
			else
			{
				g_s_rev_state_coap = S0;
			}
		}
		break;
		case S2:
		{
			if (data == 'N')
			{
				g_s_rev_state_coap = S3;
			}
			else
			{
				g_s_rev_state_coap = S0;
			}
		}
		break;
		case S3:
		{
			if (data == 'M')
			{
				g_s_rev_state_coap = S4;
			}
			else
			{
				g_s_rev_state_coap = S0;
			}
		}
		break;
		case S4:
		{
			if (data == 'I')
			{
				g_s_rev_state_coap = S5;
			}
			else
			{
				g_s_rev_state_coap = S0;
			}
		}
		break;    
		case S5:
		{
			if(data == ':')
			{
				g_s_rev_state_coap = S6;
			}
			else
			{
				g_s_rev_state_coap = S0;
			}
		}
		break; 
		case S6:
		{
			if (data == ',')
			{
				if (rev_count == 4)
				{
					rev_len = gprs_rev_len[0]*1000 + gprs_rev_len[1]*100 + gprs_rev_len[2]*10 + gprs_rev_len[3];
				}
				else if (rev_count == 3)
				{
					rev_len = gprs_rev_len[0]*100 + gprs_rev_len[1]*10 + gprs_rev_len[2];
				}
				else if (rev_count == 2)
				{
					rev_len = gprs_rev_len[0]*10 + gprs_rev_len[1];
				}
				else if (rev_count == 1)
				{
					rev_len = gprs_rev_len[0];
				}
				rev_count = 0;
				Uart_Comm.rev_len = 0;
				g_s_rev_state_coap = S7;
			}
			else
			{ 
				if(data >= '0' && data <= '9')
				{
					gprs_rev_len[rev_count++] = (data - 0x30);
				}
			}  
		}
		break;
		case S7:
		{
			if (Uart_Comm.rev_len <= BC25_UP_BUF_SIZE * 2)
			{
				Uart_Comm.rev_buf[Uart_Comm.rev_len++] = data;
				if ((Uart_Comm.rev_len >= rev_len * 2)||(data == 0x0D))
				{
					Uart_Comm.nb_data_type = NB_RCV_COAP_DATA;
					Uart_Comm.nbiot_rev_flag = TRUE; 
					g_s_rev_state_coap = S0;
				}
			}
			else
			{
				g_s_rev_state_coap = S0;
			}	   
		}
		break; 
		default:
		{
			g_s_rev_state_coap = S0;
		}
		break;
    }
}
//static void AT_NBIOT_REV_MACHINE1(u8 data)
//{
//    switch (g_s_rev_state_coap)
//    {
//        case S0:
//        {         
//            if (data == 'N')
//            {
//                g_s_rev_state_coap = S2;
//            }
//            else 
//			{
//				g_s_rev_state_coap = S0;
//			}
//        }
//        break;

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

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

//        case S4:
//        {
//            if (data == 'I')
//            {
//                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 (data >= 0x30 && data <= 0x39)
//                {
//					if(Uart_Comm.nbiot_rev_serial_number_cnt<9)
//					{
//					     Uart_Comm.nbiot_rev_serial_number[Uart_Comm.nbiot_rev_serial_number_cnt++] = data;
//					}
//					else
//					{
//					     Uart_Comm.nbiot_rev_serial_number_cnt = 0;
//					}
//                }
//            }
//            else
//            {
//				Uart_Comm.rev_len = 0;
//                g_s_rev_state_coap = S7; //第2个','
//            }
//        }
//        break;

//        case S7:
//        {
//            if ((data != ',')&&(data != 0x0D))//  ',' 或者 回车
//            {
//                if (Uart_Comm.rev_len < BC25_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;
//                Uart_Comm.nbiot_rev_flag = TRUE; 
//                g_s_rev_state_coap = S0;
//            }
//        }
//        break;

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

static void AT_CSQ_MACHINE(u8 data)
{
    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 == 'S')
            {
                g_s_rev_state = S3;
            }
            else
            {
                g_s_rev_state = S0;
            }
        }
        break;

        case S3:
        {
            if (data == 'Q')
            {
                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:
        {
            g_csq_value = data - 0x30; //存储的为Hex数据
            g_s_rev_state = S6;
        }
        break;

        case S6:
        {
            if (data == ',') //信号值为一位数 0x2C ->","
            {
                g_s_rev_state = S7;
            }
            else //信号值为两位数
            {
                g_csq_value = (g_csq_value * 10) + (data - 0x30);
                g_s_rev_state = S7;
            }
        }
        break;

        case S7:
        {
            if (data == 'O')
            {
                g_s_rev_state = S8;
                if (g_csq_value > 31)
                {
                    g_csq_value = 0;
                }
            }
        }
        break;

        case S8:
        {
            if (data == 'K')
            {
                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_CGSN_MACHINE(u8 data)
//{
//    static u8 count = 0;
//    switch (g_s_rev_state)
//    {
//        case S0:
//        {
//            if(data == '+' || data == 'O')
//            {
//                g_s_rev_state = S1;
//            }
//            break;
//        }
//        case S1:
//        {
//            if(data == 'C')
//            {
//                g_s_rev_state = S2;
//            }
//            else if(data == 'R')
//            {
//                Uart_Comm.rev_ack_flag = UART_ERROR;
//                g_s_rev_state = S0;
//            }
//            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 == ':')
//            {
//                count = 0;
//                g_s_rev_state = S6;
//            }
//            else
//            {
//                g_s_rev_state = S0;
//            }
//            break;
//        }
//        case S6:
//        {
//            if(count == 0)
//            {
//                IMEI_num[count] = (data - 0x30);
//                count++;
//                g_s_rev_state = S6;
//            }
//            else
//            {
//                IMEI_num[count] = (data - 0x30)<<4;
//                g_s_rev_state = S7;
//            }
//
//            break;
//        }
//        case S7:
//        {
//            IMEI_num[count] += (data - 0x30);
//            count++;
//            if(count < 8)
//            {
//                g_s_rev_state = S6;
//            }
//            else
//            {
//                Uart_Comm.rev_ack_flag = UART_OK;
//                g_s_rev_state = S0;
//            }
//            break;
//        }
//    }
//}
//+QENG: 0,3688,,5,"5795b2b",-77.04,3,-53,-12,8,"3f2b",1,10
//static void AT_NUWSTATS_MACHINE1(u8 data)
//{
//    switch (g_s_rev_state)
//    {
//        case S0:
//        {
//            if(data == ',') //
//            {
//                s_g_temp1 = 0;
//                s_g_earfcn_value = 0;
//                g_s_rev_state = S1;
//            }
//        }
//        break;

//        case S1://earfcn
//        {
//            if(data == ',') //earfcn
//            {
//                int tmp1=0;
//                s_g_earfcn_value = 0;
//                for(tmp1 = 0 ; tmp1 < s_g_temp1 ; tmp1++)
//                {
//                    s_g_earfcn_value *= 10;
//                    s_g_earfcn_value += g_tmp_byte[tmp1];
//                }
//                s_g_temp1 = 0;
//                g_s_rev_state = S2;
//            }
//            else
//            {
//                if(s_g_temp1 < 10)
//                {
//                    g_tmp_byte[s_g_temp1] = data - 0x30;
//                    s_g_temp1++;
//                }
//                else
//                {
//                    g_s_rev_state = S_IDLE;
//                }
//            }
//        }
//        break;

//        case S2:
//        {
//            if(data == ',') //earfcn_offset
//            {
//                int tmp1=0;
//                s_g_earfcn_offset_value = 0;
//                for(tmp1 = 0 ; tmp1 < s_g_temp1 ; tmp1++)
//                {
//                    s_g_earfcn_offset_value *= 10;
//                    s_g_earfcn_offset_value += g_tmp_byte[tmp1];
//                }
//                s_g_temp1 = 0;
//                g_s_rev_state = S3;
//            }
//            else
//            {
//                if(s_g_temp1 < 10)
//                {
//                    g_tmp_byte[s_g_temp1] = data - 0x30;
//                    s_g_temp1++;
//                }
//                else
//                {
//                    g_s_rev_state = S_IDLE;
//                }
//            }
//        }
//        break;

//        case S3://PCI
//        {
//            if(data == ',')//pci
//            {
//                int tmp = 0;
//                s_g_pci_value = 0;
//                for(tmp = 0 ; tmp < s_g_temp1 ; tmp++)
//                {
//                    s_g_pci_value *= 10;
//                    s_g_pci_value += g_tmp_byte[tmp];
//                }
//                g_s_rev_state = S4;
//            }
//            else
//            {
//                if(s_g_temp1 < 5)
//                {
//                    g_tmp_byte[s_g_temp1] = data - 0x30;
//                    s_g_temp1++;
//                }
//                else
//                {
//                    g_s_rev_state = S_IDLE;
//                }
//            }
//        }
//        break;

//        case S4:
//        {
//            if(data == '"')
//            {
//                s_g_temp1 = 0;
//                g_s_rev_state = S5;
//            }
//        }
//        break;

//        case S5://CELLID
//        {
//            if(data == '"')//
//            {
//                int tmp=0;
//                memset(s_g_cell_id,0,CELL_ID_SIZE);
//                for(tmp=0 ; tmp < s_g_temp1 ; tmp++)
//                {
//                    s_g_cell_id[tmp] = g_tmp_byte[tmp];
//                }
//                g_s_rev_state = S6;
//            }
//            else
//            {
//                if(s_g_temp1 < 10)
//                {
//                    g_tmp_byte[s_g_temp1] = data;//cellid 存字符
//                    s_g_temp1++;
//                }
//                else
//                {
//                    g_s_rev_state = S_IDLE;
//                }
//            }
//        }
//        break;

//        case S6:
//        {
//            if(data == ',')
//            {
//                s_g_temp1=0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S7;
//            }
//        }
//        break;

//        case S7:
//        {
//            if(data == '.')//rsrp
//            {
//                if(1 == s_g_temp1)
//                {
//                    s_g_rsrp_value = g_tmp_byte[1];
//                }
//                else if(2 == s_g_temp1)
//                {
//                    s_g_rsrp_value = g_tmp_byte[1] * 10 + g_tmp_byte[2];
//                }
//                else if(3 == s_g_temp1)
//                {
//                    s_g_rsrp_value = g_tmp_byte[1] * 100 + g_tmp_byte[2] * 10 + g_tmp_byte[3];
//                }
//                else if(4 == s_g_temp1)
//                {
//                    s_g_rsrp_value = g_tmp_byte[1] * 1000 + g_tmp_byte[2] * 100 + g_tmp_byte[3]*10 + g_tmp_byte[4];
//                }
//                s_g_rsrp_value=s_g_rsrp_value*10;
//                if(1 == g_tmp_byte[0])//负数
//                {
//                    s_g_rsrp_value = -s_g_rsrp_value;
//                }
//                g_s_rev_state = S8;
//            }
//            else
//            {
//                if(data == '-')
//                {
//                    g_tmp_byte[0] = 1;
//                }
//                else
//                {
//                    if(s_g_temp1 < 4)
//                    {
//                        g_tmp_byte[1 + s_g_temp1] = data - 0x30;
//                        s_g_temp1++;
//                    }
//                }
//            }
//        }
//        break;

//        case S8:
//        {
//            if(data == ',')
//            {
//                s_g_temp1=0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S9;
//            }
//        }
//        break;

//        case S9:
//        {
//            if(data == ',')//rsrq
//            {
//                if(1 == s_g_temp1)
//                {
//                    s_g_rsrq_value = g_tmp_byte[1];
//                }
//                else if(2 == s_g_temp1)
//                {
//                    s_g_rsrq_value = g_tmp_byte[1] * 10 + g_tmp_byte[2];
//                }
//                else if(3 == s_g_temp1)
//                {
//                    s_g_rsrq_value = g_tmp_byte[1] * 100 + g_tmp_byte[2] * 10 + g_tmp_byte[3];
//                }
//                else if(4 == s_g_temp1)
//                {
//                    s_g_rsrq_value = g_tmp_byte[1] * 1000 + g_tmp_byte[2] * 100 + g_tmp_byte[3]*10 + g_tmp_byte[4];
//                }
//                s_g_rsrq_value=s_g_rsrq_value*10;
//                if(1 == g_tmp_byte[0])//负数
//                {
//                    s_g_rsrq_value = -s_g_rsrq_value;
//                }
//                s_g_temp1 = 0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S10;
//            }
//            else
//            {
//                if(data == '-')
//                {
//                    g_tmp_byte[0] = 1;
//                }
//                else
//                {
//                    if(s_g_temp1 < 4)
//                    {
//                        g_tmp_byte[1 + s_g_temp1] = data - 0x30;
//                        s_g_temp1++;
//                    }
//                }
//            }
//        }
//        break;

//        case S10:
//        {
//            if(data == ',')//rssi
//            {
//                if(1 == s_g_temp1)
//                {
//                    s_g_rssi_value = g_tmp_byte[1];
//                }
//                else if(2 == s_g_temp1)
//                {
//                    s_g_rssi_value = g_tmp_byte[1] * 10 + g_tmp_byte[2];
//                }
//                else if(3 == s_g_temp1)
//                {
//                    s_g_rssi_value = g_tmp_byte[1] * 100 + g_tmp_byte[2] * 10 + g_tmp_byte[3];
//                }
//                else if(4 == s_g_temp1)
//                {
//                    s_g_rssi_value = g_tmp_byte[1] * 1000 + g_tmp_byte[2] * 100 + g_tmp_byte[3]*10 + g_tmp_byte[4];
//                }
//                s_g_rssi_value=s_g_rssi_value*10;
//                if(1 == g_tmp_byte[0])//负数
//                {
//                    s_g_rssi_value = -s_g_rssi_value;
//                }
//                s_g_temp1 = 0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S11;
//            }
//            else
//            {
//                if(data == '-')
//                {
//                    g_tmp_byte[0] = 1;
//                }
//                else
//                {
//                    if(s_g_temp1 < 4)
//                    {
//                        g_tmp_byte[1 + s_g_temp1] = data - 0x30;
//                        s_g_temp1++;
//                    }
//                }
//            }
//        }
//        break;

//        case S11://SNR
//        {
//            if(data == ',')//
//            {
//                if(1 == s_g_temp1)
//                {
//                    s_g_snr_value = g_tmp_byte[1];
//                    s_g_snr_value = s_g_snr_value*10;
//                }
//                else if(2 == s_g_temp1)
//                {
//                    s_g_snr_value = g_tmp_byte[1] * 10 + g_tmp_byte[2];
//                    s_g_snr_value = s_g_snr_value*10;
//                }
//                else if(3 == s_g_temp1)
//                {
//                    s_g_snr_value = g_tmp_byte[1] * 100 + g_tmp_byte[2] * 10 + g_tmp_byte[3];
//                    s_g_snr_value = s_g_snr_value*10;
//                }
//                else if(4 == s_g_temp1)
//                {
//                    s_g_snr_value = g_tmp_byte[1] * 1000 + g_tmp_byte[2] * 100 + g_tmp_byte[3]*10 +g_tmp_byte[4];
//                    s_g_snr_value = s_g_snr_value*10;
//                }
//                if(1 == g_tmp_byte[0])
//                {
//                    s_g_snr_value = -s_g_snr_value;
//                }
//                s_g_temp1 = 0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S12;
//            }
//            else
//            {
//                if(data == '-')
//                {
//                    g_tmp_byte[0] = 1;
//                }
//                else
//                {
//                    if(s_g_temp1 < 4)
//                    {
//                        g_tmp_byte[s_g_temp1 + 1] = data - 0x30;
//                        s_g_temp1++;
//                    }
//                    else
//                    {
//                        g_s_rev_state = S0;
//                    }
//                }
//            }
//        }
//        break;

//        case S12:
//        {
//            if(data == ',')//band
//            {
//                int tmp = 0;
//                s_g_band_value = 0;
//                for(tmp = 0 ; tmp < s_g_temp1 ; tmp++)
//                {
//                    s_g_band_value *= 10;
//                    s_g_band_value += g_tmp_byte[tmp];
//                }
//                s_g_temp1 = 0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S13;
//            }
//            else
//            {
//                if(s_g_temp1 < 4)
//                {
//                    g_tmp_byte[s_g_temp1] = data - 0x30;
//                    s_g_temp1++;
//                }
//                else
//                {
//                    g_s_rev_state = S0;
//                }
//            }
//        }
//        break;

//        case S13:
//        {
//            if(data == '"')
//            {
//                s_g_temp1=0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S14;
//            }
//        }
//        break;

//        case S14:
//        {
//            if(data == '"')//tac
//            {
//                int tmp=0;
//                for(tmp=0 ; tmp < s_g_temp1 ; tmp++)
//                {
//                    s_g_tac[tmp] = g_tmp_byte[tmp];
//                }
//                s_g_temp1=0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S15;
//            }
//            else
//            {
//                if(s_g_temp1 < 10)
//                {
//                    g_tmp_byte[s_g_temp1] = data;//cellid 存字符
//                    s_g_temp1++;
//                }
//                else
//                {
//                    g_s_rev_state = S_IDLE;
//                }
//            }
//        }
//        break;
//		
//        case S15:
//        {
//            if(data == ',')//
//            {
//                s_g_temp1=0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S16;
//            }
//        }
//        break;

//        case S16://范围0~2
//        {
//            if(data == ',')//ecl
//            {
//                s_g_ecl_value = g_tmp_byte[0];
//                g_s_rev_state = S17;
//            }
//            else
//            {
//				g_tmp_byte[0] = data - 0x30;
//            }
//        }
//        break;

//        case S17://tx_pwr
//        {
//            if(data == '-')
//            {
//                g_tmp_byte[0] = 1;
//            }
//            else if( (data >= 0x30) && (data <= 0x39))
//            {
//                if(s_g_temp1 < 4)
//                {
//                    g_tmp_byte[s_g_temp1 + 1] = data - 0x30;
//                    s_g_temp1++;
//                }
//                else
//                {
//                    g_s_rev_state = S0;
//                }
//            }
//            else
//            {
//                if(1 == s_g_temp1)
//                {
//                    s_g_tx_pwr = g_tmp_byte[1];
//                    s_g_tx_pwr = s_g_tx_pwr*10;
//                }
//                else if(2 == s_g_temp1)
//                {
//                    s_g_tx_pwr = g_tmp_byte[1] * 10 + g_tmp_byte[2];
//                    s_g_tx_pwr = s_g_tx_pwr*10;
//                }
//                if(3 == s_g_temp1)
//                {
//                    s_g_tx_pwr = g_tmp_byte[1] * 100 + g_tmp_byte[2] *10 + g_tmp_byte[3];
//                    s_g_tx_pwr = s_g_tx_pwr*10;
//                }
//                else if(4 == s_g_temp1)
//                {
//                    s_g_tx_pwr = g_tmp_byte[1] * 1000 + g_tmp_byte[2] *100 + g_tmp_byte[3] * 10 + g_tmp_byte[4];
//                    s_g_tx_pwr = s_g_tx_pwr*10;
//                }
//                if(1 == g_tmp_byte[0])
//                {
//                    s_g_tx_pwr = -s_g_tx_pwr;
//                }
//                s_g_temp1 = 0;
//                g_tmp_byte[0] = 0;
//                g_s_rev_state = S0;
//                Uart_Comm.rev_ack_flag = UART_OK;
//            }
//        }
//        break;

//        default:
//        {
//            g_s_rev_state = S0;
//        }
//        break;
//    }
//}
static void AT_NUWSTATS_MACHINE(u8 data)
{
    switch (g_s_rev_state)
     {
        case S0:
        {
            if(data == ':')
            {
                s_g_temp1 = 0;
                g_ecl_snr_value[0] = 0;
                g_s_rev_state = S1;
            }
            break;
        }
        case S1:
        {
            if((data != '-') && ((data < 0x30) || (data > 0x39)))//换行
             {
                if(1 == s_g_temp1)
                {
                    s_g_rsrp_value = g_ecl_snr_value[1];
                }
                else if(2 == s_g_temp1)
                {
                    s_g_rsrp_value = g_ecl_snr_value[1] * 10 + g_ecl_snr_value[2];
                }
                else if(3 == s_g_temp1)
                {
                    s_g_rsrp_value = g_ecl_snr_value[1] * 100 + g_ecl_snr_value[2] * 10 + g_ecl_snr_value[3];
                }
                else if(4 == s_g_temp1)
                {
                    s_g_rsrp_value = g_ecl_snr_value[1] * 1000 + g_ecl_snr_value[2] * 100 + g_ecl_snr_value[3]*10 + g_ecl_snr_value[4];
                }
				if(g_ecl_snr_value[0] == 1)
				{
					s_g_rsrp_value = -s_g_rsrp_value;
				}
				if(s_g_rsrp_value==0)
				{
					g_s_rev_state = S0;
				}
				else
				{
					g_s_rev_state = S9;
				} 
             }
             else
             {
                if(data == '-')
                    g_ecl_snr_value[0] = 1;
                else
                {                    
                    if(s_g_temp1 < 4)
                    {
                        g_ecl_snr_value[1 + s_g_temp1] = data - 0x30;
                        s_g_temp1++;
                    }
                }
             }
            break;
        }
		 case S9:	
		 {
			if(data == 'I')
			{
			    g_s_rev_state = S10;
			}
			break;		 		 
		 }
         case S10:
		 {
            if(data == 'D')
            {
                g_s_rev_state = S11;
            }
            else
            {
                g_s_rev_state = S_IDLE;
            }
            break;
		 }	
		case S11:
		{
			if(data == ':')
			{
				s_g_temp1 = 0;
				s_g_cell_id[0] = 0;
				g_s_rev_state = S12;
			}
			else
			{
				g_s_rev_state = S_IDLE;
			}
			break;
		}
		case S12:
		{
			if((data != '-') && ((data < 0x30) || (data > 0x39)))//换行
			{
				int tmp=0;
				s_g_cell_id_value = 0;
				for(tmp=0 ; tmp < s_g_temp1 ;tmp++)
				{
		            s_g_cell_id_value *= 10;
                    s_g_cell_id_value += s_g_cell_id[tmp + 1];
				}
				g_s_rev_state = S2;
			}
			else
			{
				if(data == '-')
				{
					s_g_cell_id[0] = 1;
				}
				else
				{                   
					if(s_g_temp1 < 10)
					{
						s_g_cell_id[1 + s_g_temp1] = data; 
						s_g_temp1++;
					}
					else
					{
						g_s_rev_state = S_IDLE;
					}
				}
			}
			break;
		}		
		case S2:
		{
			if(data == 'E')
			{
			    g_s_rev_state = S3;
			}
			break;
		}
        case S3:
        {
            if(data == 'C')
             {
                 g_s_rev_state = S4;
             }
             else
             {
                 g_s_rev_state = S2;
             }
            break;
        }
        case S4:
        {
            if(data == 'L')
             {
                 g_s_rev_state = S5;
             }
             else
             {
                 g_s_rev_state = S0;
             }
            break;
        }
        case S5:
        {
            if(data == ':')
             {
                s_g_temp1 = 0;
                g_ecl_snr_value[0] = 0;
                 g_s_rev_state = S6;
             }
             else
             {
                 g_s_rev_state = S0;
             }
            break;
        }
        case S6:
        {
            if((data >= 0x30) && (data <= 0x39))//换行
             {
				s_g_ecl_value = data - 0x30;
                g_s_rev_state = S7;
             }
             else
             {
                g_s_rev_state = S7;
             }
            break;
        }
        case S7:
        {
            if(data == ':')
             {
                s_g_temp1 = 0;
                g_ecl_snr_value[0] = 0;
                 g_s_rev_state = S8;
             }
            break;
        }
        case S8://SNR
        {
			if(data == '-')
			{
				g_ecl_snr_value[0] = 1;
			} 
			else if((data >= 0x30) && (data <= 0x39))//数字
			{                   
				if(s_g_temp1 < 4)
				{
					g_ecl_snr_value[1 + s_g_temp1] = data - 0x30;
					s_g_temp1++;
				}
				else
				{
					g_s_rev_state = S0;
				}
			}
			else
			{
				if(1 == s_g_temp1)
				{
					s_g_snr_value = g_ecl_snr_value[1];
				}
				else if(2 == s_g_temp1)
				{
					s_g_snr_value = g_ecl_snr_value[1] * 10 + g_ecl_snr_value[2];
				}
				else if(3 == s_g_temp1)
				{
					s_g_snr_value = g_ecl_snr_value[1] * 100 + g_ecl_snr_value[2] * 10 + g_ecl_snr_value[3];
				}
				else if(4 == s_g_temp1)
				{
					s_g_snr_value = g_ecl_snr_value[1] * 1000 + g_ecl_snr_value[2] * 100 + g_ecl_snr_value[3]*10 +g_ecl_snr_value[4];
				}
				if(1 == g_ecl_snr_value[0])
				{
					s_g_snr_value = -s_g_snr_value;
				}
				 g_s_rev_state = S13;
			}
            break;
        }
		case S13://earfcn
        {
            if(data == ':')
            {
                s_g_temp1 = 0;
                g_ecl_snr_value[0] = 0;
                g_s_rev_state = S14;
            }
            break;
        }
        case S14:
        {
            if((data != '-') && ((data < 0x30) || (data > 0x39)))//换行
            {
                int tmp1=0;
                s_g_earfcn_value = 0;
                for(tmp1 = 0 ; tmp1 < s_g_temp1 ;tmp1++)
                {
                    s_g_earfcn_value *= 10;
                    s_g_earfcn_value += g_ecl_snr_value[tmp1];
                }
                g_s_rev_state = S15;
            }
            else
            {                
                if(s_g_temp1 < 10)
                {
                    g_ecl_snr_value[s_g_temp1] = data - 0x30;
                    s_g_temp1++;
                }
                else
                {
                    g_s_rev_state = S_IDLE;
                }
            }
            break;
        }                     
        case S15:   //PCI
        {
            if(data == 'I')
            {
                g_s_rev_state = S16;
            }
            break;
        }
        case S16:
        {
            if(data == ':')
            {
                s_g_temp1 = 0;
                g_ecl_snr_value[0] = 0;
                g_s_rev_state = S17;
            }
            else
            {
                g_s_rev_state = S_IDLE;
            }
            break;
        }
        case S17:
        {
            if((data != '-') && ((data < 0x30) || (data > 0x39)))//??
             {
                int tmp = 0;
                s_g_pci_value = 0;
                for(tmp = 0 ; tmp < s_g_temp1 ;tmp++)
                {
                    s_g_pci_value *= 10;
                    s_g_pci_value += g_ecl_snr_value[tmp + 1];
                }
//                 g_s_rev_state = S18;
                 Uart_Comm.rev_ack_flag = UART_OK;
                 g_s_rev_state = S_IDLE;
             }
             else
             {
                if(data == '-')
                    g_ecl_snr_value[0] = 1;
                else
                {                   
                    if(s_g_temp1 < 3)
                    {
                        g_ecl_snr_value[1 + s_g_temp1] = data - 0x30;
                        s_g_temp1++;
                    }
                    else
                    {
                        g_s_rev_state = S_IDLE;
                    }
                }
             }
            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'))
                {
                    Nbiot_Para.imei[rev_count++] = data;
                }
            }
            else
            {
                rev_count = 0;
                g_s_rev_state = S0;
                Nbiot_Para.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) = Nbiot_Para.imei[i];
    }
}
void NbiotGetIMSI(char *imsi)
{
    u8 i = 0;
    for (i = 0; i < 15; i++)
    {
        *(imsi + i) = Nbiot_Para.imsi[i];
    }
}
void NbiotGetICCID(char *iccid)
{
    u8 i = 0;
    for (i = 0; i < 20; i++)
    {
        *(iccid + i) = Nbiot_Para.iccid[i];
    }
}

/*static void AT_CHECK_ERROR_NUM_MACHINE(u8 data)
{
    static u8 check_error_state = S0;
    static u16 err_num;
    switch (check_error_state)
    {
        case S0:
        {
            if (data == 'E')
            {
                check_error_state = S1;
                err_num = 0;
            }
        }
        break;

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

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

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

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

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

        case S6:
        {
            if (data != 0x0D)
            {
                if (data >= '0' && data <= '9')
                {
                    err_num = err_num * 10;
                    err_num += (data - 0x30);
                }
            }
            else
            {
                check_error_state = S0;
            }
        }
        break;

        default:
        {
            check_error_state = S0;
        }
        break;
    }
}*/

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;//BC28无双引号
            }
            else
            {
                get_cclk_state = S0;
            }
        }
        break;

        case S5:
        {
            if (data == ':')
            {
                get_cclk_state = S6;
                temp_data = 0;
            }
            else
            {
                get_cclk_state = S0;
            }
        }
        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;
    }
}

//查询当前SIM卡的ICCID号,+NCCID:4412345678901234567\r\n
//BC28 AT+NCCID  +NCCID:89860322472002429077
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 <= 'F')) //数字
                    {
                        Nbiot_Para.iccid[rev_count++] = data;
                    }
                }
                else
                {
                    rev_count = 0;
                    Nbiot_Para.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;
    }
}
/**
[18:24:16.919]收←◆AT+MIPLOPEN=0,600,60
OK
[18:24:17.383]收←◆
+MIPLEVENT: 0,4
[18:24:18.118]收←◆
+MIPLEVENT: 0,6
+MIPLOBSERVE: 0,119438,1,3200,0,-1
[18:24:18.662]收←◆
+MIPLOBSERVE: 0,184975,1,3200,1,-1
[18:24:19.287]收←◆
+MIPLDISCOVER: 0,53904,3200
*/
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;
            //else g_s_rev_state = S0;
        }
        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;
                g_s_rev_state = S_IDLE; //检测到结果,置空状态
            }
            else
            {
                g_s_rev_state = S0; //Bootstrap 流程处理成功 继续等待

            }
        }
        break;
        default:
            break;
    }
}

static void AT_WAIT_MIPLDISCOVER(u8 data) //+MIPLDISCOVER: 0,59745,3202
{
    static u8 wait_mipldiscover_state = S0;
	static u8 point_num;
    switch (wait_mipldiscover_state)
    {
        case S0:
        {
            if (data == 'D') wait_mipldiscover_state = S1;
            else wait_mipldiscover_state = S0;
        }
        break;
        case S1:
        {
            if (data == 'I') 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 == 'C') wait_mipldiscover_state = S4;
            else wait_mipldiscover_state = S0;
        }
        break;
        case S4:
        {
            if (data == 'O') 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') wait_mipldiscover_state = S7;
            else wait_mipldiscover_state = S0;
        }
        break;
        case S7:
        {
            if (data == 'R') wait_mipldiscover_state = S8;
            else wait_mipldiscover_state = S0;
        }
        break;
        case S8:
        {
            if (data == ':') wait_mipldiscover_state = S9;
            else wait_mipldiscover_state = S0;
        }
        break;		
        case S9:
        {
            if (data == ',') 
			{
				point_num = 0;
				memset(Uart_Comm.nbiot_discover_num,0,sizeof(Uart_Comm.nbiot_discover_num));
				wait_mipldiscover_state = S10;
			}
        }
        break;
        case S10:
        {
            if (data != ',') 
			{
				if(point_num < sizeof(Uart_Comm.nbiot_discover_num))
				{
					Uart_Comm.nbiot_discover_num[point_num++] = data;
				}
			}
			else //检测到第二个","
			{
				Uart_Comm.rev_ack_flag = UART_OK;
				wait_mipldiscover_state = S0;
			}
        }
        break;	
		default:
		{
			wait_mipldiscover_state = S0;
		}
		break;      		
    }
}
//移动接收数据
void AT_CM_NBIOT_REV_MACHINE(u8 data)
{
	static u8 g_s_rev_state_onenet = S0;
    switch (g_s_rev_state_onenet)
    {
    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=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;
        }
        //else g_s_rev_state=S0;
    }
        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(data >= 0x30 && data <= 0x39) 
            {
				if(Uart_Comm.nbiot_rev_serial_number_cnt<9)
				{
				    Uart_Comm.nbiot_rev_serial_number[Uart_Comm.nbiot_rev_serial_number_cnt++]=data;
				}
            }
        }
        else
        {
            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 < BC25_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.nb_data_type = NB_RCV_COAP_DATA;
            Uart_Comm.nbiot_rev_ack = TRUE;//先做写应答然后处理数据
            g_s_rev_state_onenet = S0;
         }
    }
        break;

    default:
        break;
    }
}

static void AT_MIPLCLOSE_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;
            //else g_s_rev_state = S0;
        } break;   
 
         case S7:
        {
            if (data == ',')  g_s_rev_state = S8;
            else g_s_rev_state = S0;
        } break;   
         case S8:
        {
            if (data == '1')  g_s_rev_state = S9;
            else g_s_rev_state = S0;
        } break;   
         case S9:
        {
            if (data == '5')  
            {
                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; //检测到结果,置空状态
	     }break;
     }
}

//+NSMI:SENT_TO_AIR_INTERFACE,100
static void AT_NMGS_ACKID_MACHINE(u8 data)
{
    switch (g_s_rev_state)
    { 
        case S0:
        {
            if (data == 'F')  g_s_rev_state = S1;
            else g_s_rev_state = S0;
        } break;     
 
        case S1:
        {
            if (data == 'A')  g_s_rev_state = S2;
            else g_s_rev_state = S0;
        } break; 
 
        case S2:
        {
            if (data == 'C')  g_s_rev_state = S3;
            else g_s_rev_state = S0;
        } break; 
        
         case S3:
        {
            if (data == 'E')  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 == '1')  g_s_rev_state = S6;
            else g_s_rev_state = S0;
        } break;           
         case S6:
        {
            if (data == '0')  
            {
				FrameSetMsgApp(MsgNbUplaod_AckOk);
                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;
		 }break;
     }	
}
static void AT_MIPLNOTIFY_ACKID_MACHINE(u8 data)
{
	static u16 ackID_temp;
	switch (g_s_rev_state)
	{
		case S0:
		{
			if (data == ',')
			{
				g_s_rev_state = S1;
				ackID_temp = 0;
			}
			else g_s_rev_state = S0;
		}
		break;
		case S1:
		{
			if (data != ',')
			{
				if ((data >= '0') && (data <= '9'))
				{
					ackID_temp *= 10;
					ackID_temp += (data - 0x30);
					if (ackID_temp == 26)//比对成功 
					{
						g_s_rev_state = S2;
						ackID_temp = 0;
					}
				}
			}
			else g_s_rev_state = S0;
		}
		break;
		case S2:
		{
			if (data != 0x0D)
			{
				if ((data >= '0') && (data <= '9'))
				{
					ackID_temp *= 10;
					ackID_temp += (data - 0x30);
					if (ackID_temp == get_Send_AckID())//比对成功 
					{
						FrameSetMsgApp(MsgNbUplaod_AckOk);
						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;
	}
}
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)
{
	g_s_udprev_state = S0;
	udp_check_rev_data_flag = TRUE;
}
//udp结束查询数据
void UdpEndCheckData(void)
{
	udp_check_rev_data_flag = FALSE;
}
//udp是否需要读数据
u8 UdpIfNeedReadDataBc25(u8 connect_type)
{
	if(connect_type == NBIOT_CONNECT_UDP1)
	{
		return Uart_Comm.udp1_rev_data_hint;
	}
	else
	{
		return Uart_Comm.udp2_rev_data_hint;
	}
}
u8 UdpIfNeedReadData(void)
{
	return (Uart_Comm.udp1_rev_data_hint|Uart_Comm.udp2_rev_data_hint);
}
//清楚udp接收数据标志
void ClearUdpReadDataFlag(u8 connect_type)
{
	if(connect_type == NBIOT_CONNECT_UDP1)
	{
		Uart_Comm.udp1_rev_data_hint = FALSE;
	}
	else
	{
		Uart_Comm.udp2_rev_data_hint = FALSE;
	}
}
//获取收到的数据类型
u8 GetRcvDataType(void)
{
	return Uart_Comm.nb_data_type;
}
//设置收到的数据类型
void SetRcvDataType(u8 temp_type)
{
	Uart_Comm.nb_data_type = temp_type;
}
void NbiotGetCSQ(u8 *csq_data)
{
    *csq_data = g_csq_value; 
}
void NbiotSetRevMipldiscover(void)
{
	memset(Uart_Comm.nbiot_discover_num,0,sizeof(Uart_Comm.nbiot_discover_num));
   Uart_Comm.send_style = cst_AT_No_AT_MIPLDISCOVER; //直接跳转下一个状态机  AT_WAIT_MIPLDISCOVER
}
u8 NbiotGetNBAND(void)
{
    return s_g_band_value;
}
#ifdef	__cplusplus
}
#endif

#endif

