/************************************************************************************
 * @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_ML302_TCP
#ifdef	__cplusplus
extern "C"
{
#endif
#include "nb_uart_driver_ml302.h"
#include "nb_driver_ml302.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;
u8 g_ecl_snr_value[11] = {0};
u8 g_date_time_num = 0;
u8 s_g_buf[10] = {0};
u8 s_g_temp1 = 0;

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

u8  s_g_apn_value[APN_SIZE] = {0};
u32 s_g_cell_id_value = 0;
struct tm tm_GMT_nb_uart_driver= {0,0,0,0,0,0,0,0,0}; //格林尼时间
u32 time_temp_nb_uart_driver;
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_NBIOT_REV_MACHINE(u8 data);
static void AT_CSQ_MACHINE(u8 data);
static void AT_IMEI_MACHINE(u8 data);
static void AT_GET_CCLK_MACHINE(u8 data);
static void AT_ICCID_MACHINE(u8 data);
static void AT_CPIN_MACHINE(u8 data);
static void AT_MIPOPEN_MACHINE(u8 data);
static void AT_MIPSENDCOUNT_MACHINE(u8 data);
static void AT_MIPSEND_MACHINE(u8 data);
static void AT_MIPCLOSE_MACHINE(u8 data);
static void AT_VERCTRL_CK_MACHINE(u8 data);
static void AT_TUEINFO_MACHINE(u8 data);
static void AT_CCED_MACHINE(u8 data);
/***************************************************************************
 * @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_CMEE:
        case cst_AT_No_AT_CFUN:
        case cst_AT_No_AT_CFUN0:
        case cst_AT_No_AT_CFUN1:
        {
            AT_OK_AND_ERROR_MACHINE(data); 
        }
        break;

        case cst_AT_No_AT_CIMI:
        {
            AT_CIMI_MACHINE(data);
        }
        break;

        case cst_AT_No_AT_CGACT_how:
        {
            AT_CGACT_HOW_MACHINE(data);
        }
        break;

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

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

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

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

        case cst_AT_No_AT_ICCID:
        {
            AT_ICCID_MACHINE(data);
        }
        break;	
		
		case cst_AT_No_AT_CPIN:                    
        {
            AT_CPIN_MACHINE(data); 
        }
        break;
		case cst_AT_No_AT_MIPOPEN:                    
        {
            AT_MIPOPEN_MACHINE(data); 
        }
        break;
		case cst_AT_No_AT_MIPSENDCOUNT:
		{
			AT_MIPSENDCOUNT_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_TCPSEND:
		{
			AT_MIPSEND_MACHINE(data); 
		}
		break;
		case cst_AT_No_AT_MIPCLOSE:
		{
			AT_MIPCLOSE_MACHINE(data); 
		}
		break;
		case cst_AT_No_VERCTRL_CK:
		{
			AT_VERCTRL_CK_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_TUEINFO:
		{
			AT_TUEINFO_MACHINE(data);
		}
		break;
		case cst_AT_No_AT_CCED:
		{
			AT_CCED_MACHINE(data);
		}
		
        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 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;
}
/***************************************************************************
 * @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;
		 }
	 }
}
/**
AT+CGACT?
+CGACT: 1,1
*/
#define AT_CHECK_LEN_CGACT   11
static const u8 scAT_CGACT[AT_CHECK_LEN_CGACT]="+CGACT: 1,1";
static void AT_CGACT_HOW_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_CGACT-3):
		 {
			g_s_rev_state_com = ((('1' == data)||('5' == data))? (g_s_rev_state_com+1) : 0); 
			break;
		 } 
		 case(AT_CHECK_LEN_CGACT-2):
		 {
			g_s_rev_state_com = (',' == data? (g_s_rev_state_com+1) : 0); 
			break;
		 }
		 case(AT_CHECK_LEN_CGACT-1):
		 {
			Uart_Comm.rev_ack_flag = ((data == '1')? UART_OK : UART_ERROR);
			g_s_rev_state_com = 0;
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_CGACT)
			{
				g_s_rev_state_com = (scAT_CGACT[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}

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

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

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

/**
+MIPURC: "recv",1,27
h \00!3x"G\04R	 !?
*/
/**
AT+CGACT?
+CGACT: 1,1
*/
#define AT_CHECK_LEN_MIPURC   7
static const u8 scAT_MIPURC[AT_CHECK_LEN_MIPURC]="\"recv\",";
u16 ml302_rev_data_len=0;
#define REV_STATE_MIPURC_REV_TYPE   0x10 //判断数据来源
#define REV_STATE_MIPURC_REV_LEN    0x11 //接收数据长度
#define REV_STATE_MIPURC_REV_DATA   0x12 //接收数据
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 = (scAT_MIPURC[g_s_rev_state_coap] == data? (REV_STATE_MIPURC_REV_TYPE) : 0); 
			 break;
		 }
		 case(REV_STATE_MIPURC_REV_TYPE):
		 {
			 ml302_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_LEN;
			 }
			 else if('2' == data)
			 {
				 Uart_Comm.nb_data_type = NB_RCV_UDP1_DATA;
				 g_s_rev_state_coap = REV_STATE_MIPURC_REV_LEN;
			 }
			 else if('3' == data)
			 {
				 Uart_Comm.nb_data_type = NB_RCV_UDP2_DATA;
				 g_s_rev_state_coap = REV_STATE_MIPURC_REV_LEN;
			 }
			 else
			 {
				g_s_rev_state_coap = 0;
			 }
			break;
		 }
		 case (REV_STATE_MIPURC_REV_LEN):
		 {
			 if ((data >= '0') && (data <= '9'))
			 {
				 ml302_rev_data_len *= 10;
				 ml302_rev_data_len += (data -0x30);
			 }
			 else if(0x0A == data)
			 {
				if(ml302_rev_data_len < ML302_UP_BUF_SIZE * 2)
				{
					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 < ml302_rev_data_len)
			{   
				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;
			}
			break;
		 }
	 }
}
/**
AT+CSQ
+CSQ: 14,99
*/
#define AT_CHECK_LEN_CSQ   6
static const u8 scAT_CSQ[AT_CHECK_LEN_CSQ]="+CSQ: ";
static void AT_CSQ_MACHINE(u8 data)
{
	 switch (g_s_rev_state_com)
	 {
		 case(AT_CHECK_LEN_CSQ-1):
		 {
			if (data != ',')
			{
				if (data >= '0' && data <= '9') //数字
				{
					g_csq_value *= 10;
					g_csq_value += (data - 0x30);
					g_s_rev_state_com = AT_CHECK_LEN_CSQ-1;
				}
			}
			else
			{
				Uart_Comm.rev_ack_flag = UART_OK;
				g_s_rev_state_com = 0;
			}
			break;
		 }
		 default:
		 {
			if(g_s_rev_state_com<AT_CHECK_LEN_CSQ)
			{
				g_s_rev_state_com = (scAT_CSQ[g_s_rev_state_com] == data? (g_s_rev_state_com+1) : 0); 
				g_csq_value = (':' == data? (0) : g_csq_value); 
			}
			else
			{
				g_s_rev_state_com = 0;
			}
			break;
		 }
	 }
}
/**
[14:36:32.887]收←◆AT+TUEINFO
TUEINFO:DLEARFCN,41134
TUEINFO:PCID,86
TUEINFO:RSRP,42
TUEINFO:RSRQ,17
TUEINFO:SINR,43
TUEINFO:MCL,112
TUEINFO:ULMCS,65535
TUEINFO:DLMCS,65535
TUEINFO:MPDCCHREPNUM,65535
TUEINFO:PUSCHREPNUM,65535
TUEINFO:PDSCHREPNUM,65535
TUEINFO:ULINITIALBLER,65535
TUEINFO:DLINITIALBLER,65535
TUEINFO:ULRBNUM,4294967295
TUEINFO:DLRBNUM,4294967295
TUEINFO:ULRLCRATE,4294967295
TUEINFO:DLRLCRATE,4294967295
TUEINFO:ULTBS,4294967295
TUEINFO:DLTBS,4294967295
TUEINFO:ECL,0

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

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


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

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

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

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

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

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

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

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

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

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

void NbiotGetIMEI(char *imei)
{
    u8 i = 0;
    for (i = 0; i < 15; i++)
    {
        *(imei + i) = buf_imei[i];
    }
}
void NbiotGetIMSI(char *imsi)
{
    u8 i = 0;
    for (i = 0; i < 15; i++)
    {
        *(imsi + i) = buf_imsi[i];
    }
}
void NbiotGetICCID(char *iccid)
{
    u8 i = 0;
    for (i = 0; i < 20; i++)
    {
        *(iccid + i) = buf_iccid[i];
    }
}
/**
AT+CCLK?
+CCLK: "21/09/15,09:47:26+32"
+CCLK: "21/05/13,16:37:30"
*/
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);
				}
                temp_data = 0;
                get_cclk_state = S0;
                Uart_Comm.rev_ack_flag = UART_OK;
            }
        }
        break;

        default:
        {
            get_cclk_state = S0;
        }
        break;
    }
}
/**
AT+ICCID
+ICCID: 89860438152090134392

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

			
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

