#include "at_proc.h"
#include "gsm_uart.h"
#include "gsm_uart_conf.h"
#include "my_math.h"
#include "ec200x.h"
#include "data_typedef.h"

char command_CIPSEND[16];
	
AT_CMD_PARAM at_cmd_table[]={
	{AT_CMD_AT_NO, 						"AT", 																"OK",								"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_ATE0_NO, 					"ATE0",																"OK",								"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_CGSN_NO, 					"AT+CGSN", 														"OK", 							"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_CPIN_NO, 					"AT+CPIN?",														"+CPIN: READY", 		"ERROR", 					AT_CMD_UNKNOWN},
	{AT_CMD_ICCID_NO, 				"AT+QCCID",														"+QCCID:", 					"ERROR", 					AT_CMD_UNKNOWN},
	{AT_CMD_CSQ_NO, 					"AT+CSQ",															"+CSQ:", 						"ERROR", 					AT_CMD_UNKNOWN},
	{AT_CMD_CREG_NO, 					"AT+CREG?", 													"+CREG: 0,1", 			"ERROR", 					AT_CMD_UNKNOWN},
	{AT_CMD_CGREG_NO, 				"AT+CGREG?", 													"+CGREG: 0,1", 			"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_COPS_NO, 					"AT+COPS?", 													"OK", 							"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_QICSGP_NO, 				"AT+QICSGP=1,1,\"UNINET\","","",1", 	"OK", 							"ERROR", 					AT_CMD_UNKNOWN},
	{AT_CMD_QIACT_NO,					"AT+QIACT=1",													"OK",								"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_QIDEACT_NO,				"AT+QIDEACT=1",												"OK",								"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_CHECK_QIACT_NO,		"AT+QIACT?",													"OK",								"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_QIOPEN,						TCP_IP_COM,														"+QIOPEN: 0,0",			"CONNECT FAIL",		AT_CMD_UNKNOWN},
	{AT_CMD_QICLOSE_NO,				"AT+QICLOSE=0",												"OK",								"SEND FAIL",			AT_CMD_UNKNOWN},
	{AT_CMD_QISEND,						"AT+QISEND=0,4",											">",								"SEND OK",				AT_CMD_UNKNOWN},
	{AT_CMD_CHECK_QISEND_NO,	"AT+QISEND=0,0",											"+QISEND:",					"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_QIRD_NO,					"AT+QIRD=0,1500",											"+QIRD:",						"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_QNTP_NO,					"AT+QNTP",														"+QNTP:",						"ERROR",					AT_CMD_UNKNOWN},
	{AT_CMD_CCLK_NO,					"AT+CCLK?",														"+CCLK:",						"+CME ERROR",			AT_CMD_UNKNOWN},
};

#define AT_RX_BUFFER  100
 
uint8_t rx_buffer[AT_RX_BUFFER];
uint8_t sendAtCmdNo=0;

uint8_t networkIsReg=0;
uint8_t receiveDataReady = 0;
uint8_t receiveDataLen = 0;

#define AT_COMMON_ERROR_STRING   "+CME ERROR:"



void show_buf(uint8_t num[], uint8_t n)
{
    uint8_t i;
		printf("\r\n");
    for(i=0; i<n; i++)
		{
      printf("%02X ",num[i]);
		}
		printf("\r\n");
}

/*
***********************************************************************
*	函 数 名: mem_cmp
*	功能说明: 字符串比较
*	形    参: src->原字符串
*           dst->目标字符串
*           len->字符串的长度
*	返 回 值: 0->相同,1不相同
***********************************************************************
*/
static uint8_t mem_cmp(uint8_t *src, uint8_t *dst, uint8_t len)
{
	uint8_t i = 0, ret = 0;
  
  while(i<len)
  {
    if(src[i] == 0 || dst[i] == 0 ||  src[i] != dst[i])
		{
			ret = 1;
			break;
		}  
		i++;
  }
  return ret;
}

/*
***********************************************************************
*	函 数 名: str_len
*	功能说明: 取得字符串长度
*	形    参: string->待计算的字符串
*	返 回 值: 字符串长度
***********************************************************************
*/
static uint8_t str_len(uint8_t* string)
{
   uint8_t i=0;

   while(string[i] != 0) 	i++;

   return i;
}

/*
***********************************************************************
*	函 数 名: GSM_SendAtCmd
*	功能说明: GSM模块发送AT指令
*	形    参: cmd->发送的命令字符串
*           atCmdNo->AT指令编号
*	返 回 值: 0->发送成功(得到了期待的应答结果)
*             1->发送失败
***********************************************************************
*/
void GSM_SendAtCmd(char *cmd, uint8_t atCmdNo)
{
	sendAtCmdNo = atCmdNo;
	at_cmd_table[sendAtCmdNo-1].result = AT_CMD_UNKNOWN;
	GSM_GPRS_printf("%s\r\n", cmd);				//发送命令
}

/*
***********************************************************************
*	函 数 名: getCommand_CIPSEND
*	功能说明: 根据发送的数据长度自动生成对应的AT+CIPSEND=<?>命令
*	形    参：msg
*	返 回 值: 无
***********************************************************************
*/
void getCommand_CIPSEND(uint16_t len)
{
	char lenNum[4];
	
	sprintf((char*)command_CIPSEND, "AT+CIPSEND=");
	sprintf((char*)lenNum, "%d", len);
	strcat((char*)command_CIPSEND, lenNum);
}

/*
***********************************************************************
*	函 数 名: IMEI_TO_HEX
*	功能说明: 将IMEI号转换成HEX
*	形    参：传入字符串的IMEI号	          
*	返 回 值: 
***********************************************************************
*/
void IMEI_TO_HEX(char *pStrData)
{
	uint8_t i;
	uint64_t imeiNum = 0;
	uint32_t imeiNumH = 0;
	uint32_t imeiNumL = 0;

	for(i = 0; i < 15; i++){
		imeiNum += ((*pStrData++) - 0x30) * Pow(10, 14-i);
	}
	imeiNumH = imeiNum/4294967296;
	imeiNumL = (uint32_t)imeiNum;
	
// 	printf("\r\n%x \r\n", imeiNumH);
// 	printf("\r\n%x \r\n", imeiNumL);
	
	gsm_msg.IMEI_HEX[0] = imeiNumH >> 24;
	gsm_msg.IMEI_HEX[1] = imeiNumH >> 16;
	gsm_msg.IMEI_HEX[2] = imeiNumH >> 8;
	gsm_msg.IMEI_HEX[3] = imeiNumH;
	
	gsm_msg.IMEI_HEX[4] = imeiNumL >> 24;
	gsm_msg.IMEI_HEX[5] = imeiNumL >> 16;
	gsm_msg.IMEI_HEX[6] = imeiNumL >> 8;
	gsm_msg.IMEI_HEX[7] = imeiNumL;
	
// 	for(i = 0; i < 8; i++){
// 		printf("%x ", gsm_msg.IMEI_HEX[i]);
// 	}
}


void AT_Protocol(uint8_t* pCdmStr, uint8_t length)
{
  uint8_t i = 0;
	char *p;
  uint8_t *pSuccStr;							//AT指令操作成功的应答字符串;
  uint8_t succStrLen;							
	
	uint8_t *pFailedStr;						//AT指令操作失败的应答字符串;
  uint8_t failedStrLen;
	
	if(length == 2) return; 				//空行，不处理
	
	if(sendAtCmdNo >= CMD_TABLE_LENGTH) return; 				//判断是否是前面发送过at需要处理的

  if(!((pCdmStr[length-2] == '\r') && (pCdmStr[length-1] == '\n')))		//不是回车换行（"\r\n"）结尾
	{
		if(!((sendAtCmdNo == AT_CMD_QISEND) && (GPRS_RX_BUF[length - 1] == '>'))) return;		//不是IPSEND指令的应答“>”
	}
		
	i = 0; 
  while(*pCdmStr == '\0') 	//过滤掉前面的空格
	{
		pCdmStr = pCdmStr++;
		i++;
		if(i >= length) break;
	}

  pSuccStr = at_cmd_table[sendAtCmdNo-1].success;
  succStrLen = str_len(pSuccStr); 
	
	pFailedStr = at_cmd_table[sendAtCmdNo-1].failed;
  failedStrLen = str_len(pFailedStr); 
	
	if((char*)strstr((const char*)pCdmStr, "+QIURC") != NULL)			//URC AT指令
	{
		if((char*)strstr((const char*)pCdmStr, "recv") != NULL)			//数据接收URC
		{
			if(mem_cmp(pCdmStr, (uint8_t*)RECEIVE_SERVER_DATA, strlen(RECEIVE_SERVER_DATA)) == 0)
			{
				p = (char*)strstr((const char*)pCdmStr, ",");
				p = (char*)strstr((const char*)p+1, ",");
				recDataLen = atoi(p+1);
				GSM_ReceiveType = 1;
// 				printf("\r\n接收数据URC\r\n");
// 				printf("\r\nlen=%d\r\n", recDataLen);
			}
		}else if((char*)strstr((const char*)pCdmStr, "closed") != NULL){					//连接断开URC
			connectionClosedMark = 1;
// 			printf("\r\n连接断开URC！\r\n");
		}else if((char*)strstr((const char*)pCdmStr, "incoming full") != NULL){		//客户端连接已满URC
			printf("\r\n连接已满URC！\r\n");
		}else if((char*)strstr((const char*)pCdmStr, "ncoming") != NULL){					//客户端连接URC
			printf("\r\n客户端连接URC！\r\n");
		}else if((char*)strstr((const char*)pCdmStr, "pdpdeact") != NULL){				//PDP去激活URC
			printf("\r\nPDP去激活URC！\r\n");
		}else{}
	}else{																												//常规AT指令
		switch(sendAtCmdNo)
		{
			case AT_CMD_AT_NO:		//AT
				if(mem_cmp(pCdmStr, (uint8_t*)AT_CMD_AT_AT, strlen(AT_CMD_AT_AT)) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
	// 				printf("\r\nat\r\n");
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_CMD_AT_OK, strlen(AT_CMD_AT_OK)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
	// 				printf("\r\nok\r\n");
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_ATE0_NO:		//关闭回显
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
			
			case AT_CMD_CGSN_NO:	 //AT+CGSN 获取IMEI号
				if(length == 17)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS; 				//设置当前的AT指令操作结果成功
					sendAtCmdNo = 0;																						//清除发送AT指令编号
					memcpy(gsm_msg.IMEI, pCdmStr, 15);
					gsm_msg.IMEI[15] = '\0';																		
					IMEI_TO_HEX(gsm_msg.IMEI);																	//将字符串的IMEI号转换成HEX格式为后面传输
				}
				break;
			
			case AT_CMD_CPIN_NO:		//AT+CPIN?  查询SIM卡状态
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_ICCID_NO:		//AT+QCCID  查询SIM卡的ICCID号
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					memcpy(gsm_msg.ICCID, pCdmStr+8, 20);
					gsm_msg.ICCID[20] = '\0';		
	// 				show_buf((uint8_t*)gsm_msg.ICCID, 20);
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_CSQ_NO:		//AT+CSQ  查询网络信号强度
	// 			printf("\r\ncsq = %s\r\n", pCdmStr);
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
					signal = atoi((const char *)(pCdmStr+6));
	// 				printf("\r\nrssi = %d\r\n", signal);
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
			
			case AT_CMD_CREG_NO:		//AT+CREG? 查询网络注册状态 
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)														//注册的，归属网络
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp((uint8_t*)AT_CMD_AT_CREG, pCdmStr, succStrLen) == 0){		//注册的，漫游中
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
			
			case AT_CMD_CGREG_NO:		//AT+CGREG? 检查GPRS附着状态
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)														//注册的，归属网络
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp((uint8_t*)AT_CMD_AT_CGREG, pCdmStr, succStrLen) == 0){		//注册的，漫游中
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_COPS_NO:	//查询网络运营商
				if((char*)strstr((const char*)pCdmStr, "CHINA MOBILE") != NULL){
					gsm_msg.OperatorType = 1;
					memcpy(gsm_msg.Operator, "CHINA MOBILE", strlen("CHINA MOBILE"));
				}else if((char*)strstr((const char*)pCdmStr, "CHN-UNICOM") != NULL){
					gsm_msg.OperatorType = 2;
					memcpy(gsm_msg.Operator, "CHN-UNICOM", strlen("CHN-UNICOM"));
				}else{
				}
				
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
			
			case AT_CMD_QICSGP_NO:	//开始任务，设置APN
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_QIDEACT_NO:		//反激活PDP
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
			
			case AT_CMD_QIACT_NO:	//建立无线链路
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
			
			case AT_CMD_CHECK_QIACT_NO:	 //查询当前场景及IP
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}
				break;
			
			case AT_CMD_QIOPEN:	//开始链接远端服务器
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}
				break;
				
			case AT_CMD_QISEND:		//发送数据
				if(mem_cmp((uint8_t*)SEND_STATUS_READY, pCdmStr, 1) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_CIPSEND_READY;
	// 				sendAtCmdNo = 0;
				}
				else if(mem_cmp((uint8_t*)SEND_STATUS_SEND_OK, pCdmStr, 7) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_CIPSEND_OK;
					sendAtCmdNo = 0;
				}
				else if(mem_cmp((uint8_t*)SEND_STATUS_SEND_FAIL, pCdmStr, 9) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SEND_FAIL;
					sendAtCmdNo = 0;
				}
				else if(mem_cmp((uint8_t*)AT_CMD_AT_ERROR, pCdmStr, 5) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SEND_FAIL;
					sendAtCmdNo = 0;
				}
				else if(mem_cmp((uint8_t*)SEND_STATUS_CLOSED, pCdmStr, 6) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = TCP_STATUS_CLOSED;
					sendAtCmdNo = 0;
				}
				else if(mem_cmp((uint8_t*)AT_COMMON_ERROR_STRING, pCdmStr, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SEND_FAIL;
					sendAtCmdNo = 0;				
				}else{}

				break;
				
			case AT_CMD_CHECK_QISEND_NO:		//查询发送的数据
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
	// 				printf("\r\nbyt=%s\r\n", pCdmStr);
					p = (char*)strstr((const char*)pCdmStr, ":");
					if(p != NULL)
					{
						total_send_length = atoi((const char *)(p+2));
	// 					printf("\r\nsendBytes = %d\r\n", total_send_length);
					}
					
					p = (char*)strstr((const char*)pCdmStr, ",");
					if(p != NULL)
					{
						ackedBytes = atoi((const char *)(p+1));
	// 					printf("\r\nackedBytes = %d\r\n", ackedBytes);
					}
					
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_QIRD_NO:		//读取接收到的数据
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					p = (char*)strstr((const char*)pCdmStr, ": ");
					receiveDataLen = atoi((p+1));
	// 				printf("\r\nlen=%d\r\n", receiveDataLen);
					receiveDataReady = 1;
				}else if(mem_cmp((uint8_t *)"OK", pCdmStr, 2) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					
					receiveDataReady = 0;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_QICLOSE_NO:		//"AT+CIPCLOSE=1" 关闭连接
				if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
				{
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
					sendAtCmdNo = 0;
				}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
				}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
					at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
				}else{}
				break;
				
			case AT_CMD_QNTP_NO:		//同步NTP服务器时间
			if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
			{
				at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
				sendAtCmdNo = 0;
			}else if(mem_cmp(pFailedStr, pCdmStr, failedStrLen) == 0){
				at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;
			}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
				at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
			}else{}
			break;
			
		case AT_CMD_CCLK_NO:		//查询时间
// 			printf("\r\nT=%s\r\n", pCdmStr);
			if(mem_cmp(pSuccStr, pCdmStr, succStrLen) == 0)
			{
				at_cmd_table[sendAtCmdNo-1].result = AT_CMD_SUCCESS;
				sendAtCmdNo = 0;
				
				//返回的格式为：+CCLK: "20/08/26,08:42:39+32"
				p = (char*)strstr((const char*)pCdmStr, "\"");
				
				GMT_Time.year = 2000 + (*(p+1)-'0')*10 + (*(p+2)-'0');
				GMT_Time.month = (*(p+4)-'0')*10 + (*(p+5)-'0');
				GMT_Time.day = (*(p+7)-'0')*10 + (*(p+8)-'0');
				
				GMT_Time.hour = (*(p+10)-'0')*10 + (*(p+11)-'0');
				GMT_Time.minute = (*(p+13)-'0')*10 + (*(p+14)-'0');
				GMT_Time.second = (*(p+16)-'0')*10 + (*(p+17)-'0');				
			}else if(mem_cmp(pCdmStr, (uint8_t*)AT_COMMON_ERROR_STRING, strlen(AT_CMD_AT_ERROR)) == 0){
				at_cmd_table[sendAtCmdNo-1].result = AT_CMD_FAILED;					 	 
			}else{}
			break;
			
			default:
				break;
		}
	}
}

/******************** (C) COPYRIGHT 2017 CXHY *****END OF FILE*******************/ 
