www.pudn.com > MT7681_IoT_Package_v0.02.rar > iot_at_cmd.c, change:2014-01-28,size:7292b


#include <stdio.h>
#include "string.h"
#include "types.h"
#include "uart.h"
#include "iot_custom.h"
#include "iot_api.h"

/******************************************************************************
* MODULE NAME:     iot_at_cmd.c
* PROJECT CODE:    __MT7681__
* DESCRIPTION:     
* DESIGNER:        
* DATE:            Jan 2013
*
* SOURCE CONTROL:
*
* LICENSE:
*     This source code is copyright (c) 2011 Ralink Tech. Inc.
*     All rights reserved.
*
* REVISION     HISTORY:
*   V1.0.0     Jan 2012    - Initial Version V1.0
*
*
* SOURCE:
* ISSUES:
*    First Implementation.
* NOTES TO USERS:
*
******************************************************************************/
INT8 state = 0;
INT16 cmd_len;
#define AT_CMD_MAX_LEN 100

#define SYNC_STATE_0 0
#define SYNC_STATE_1 1
#define SYNC_STATE_2 2
#define SYNC_STATE_3 3

extern 	char *optarg; 
extern  int optind; 

VOID IoT_AT_cmd_resp_header(
	IN OUT INT8 *pHeader, 
	IN OUT INT16* plen, 
	IN INT8* ATcmdPrefix,
	IN INT8* ATcmdType)
{
	INT16 len2 =0;

	/*AT command prefix*/
	*plen = strlen(ATcmdPrefix);
	memcpy(pHeader, ATcmdPrefix, *plen);

	/*AT command Type*/
	len2 = strlen(ATcmdType);
	memcpy(pHeader + *plen, ATcmdType, len2);
	*plen += len2;

	len2 = strlen("=");
	memcpy(pHeader + *plen, "=", len2);
	*plen += len2;

	//*plen += 1;
	//*(pHeader+*plen) = '=';
	
	return;
}


INT16 IoT_exec_AT_cmd_uart(PCHAR command_buffer, INT16 at_cmd_len)
{
	DBGPRINT(RT_DEBUG_INFO,("IoT_exec_ATcommand_uart: %s \n",command_buffer));
	IoT_uart_AT_config(command_buffer, at_cmd_len);
	return 0;

}

INT16 IoT_exec_AT_cmd_netmode(PCHAR command_buffer, INT16 at_cmd_len)
{
	DBGPRINT(RT_DEBUG_INFO,("IoT_exec_ATcommand_netmode: %s \n",command_buffer));
	return -1;
}

/*========================================================================
	Routine	Description:
		IoT_exec_AT_cmd_ch_switch --  switch channel
		
	Arguments:
	Return Value: 0 is success
	Note:  In present , the channel range is channel 0~13, and Bandwidth is only support 20MHz
========================================================================*/
INT16 IoT_exec_AT_cmd_ch_switch(PCHAR pCmdBuf, INT16 at_cmd_len)
{
	INT8 len, num = 0;

	len = 3;	//(INT8)strlen("-s ");
	if(!memcmp(pCmdBuf ,"-s ", len))
	{	
		pCmdBuf += len;
		at_cmd_len -= len;

		if(at_cmd_len < 0)
			return -1;

		num = (INT8)atoi(pCmdBuf);
		if (num > 14 || num < 0 )
			return -1;

		IoT_Cmd_Set_Channel((UINT8)num);
	}

	return 0;
}


/*========================================================================
	Routine	Description:
		IoT_exec_AT_cmd_reboot --  system reboot
		
	Arguments:
	Return Value:  NONE
	Note:
========================================================================*/
VOID IoT_exec_AT_cmd_reboot(VOID)
{
	Sys_reboot();
	return;
}

/*========================================================================
	Routine	Description:
		IoT_exec_AT_cmd_ver -- get FW version
		
	Arguments:
	Return Value: 0 is success,  -1 is out of memory
	Note:
========================================================================*/
INT16 IoT_exec_AT_cmd_ver(PCHAR pCmdBuf)
{
	INT16  len=0 , len2 =0;
	
	/* the response header format is:  "AT#CmdType=" */
	IoT_AT_cmd_resp_header(pCmdBuf, &len, AT_CMD_PREFIX, AT_CMD_VER);
	
	/*AT command Version*/
	len2 = strlen(FW_VERISON_CUST);
	memcpy(pCmdBuf + len, FW_VERISON_CUST, len2);
	len += len2;

	len2 = strlen("\n");
	if( (len + len2 + 3) >= AT_CMD_MAX_LEN)
	{
		/*pCmdBuf is mallocated on	ATcommandHandler(),  
		   but here it's start after "AT#"
		   thus it's length is	 (AT_CMD_MAX_LEN-3); */
		return -1;
	}
	memcpy(pCmdBuf + len, "\n", len2);
	len += len2;

	//len += 1;
	//*(pCmdBuf+len) = '\n';
	
	IoT_uart_output(pCmdBuf, len);

	return 0;
}


INT16 IoT_parse_ATcommand(PCHAR cmd_buf, INT16 at_cmd_len)
{
	INT16 ret_code = 0;
	INT8 cTypeLen = 0;

	cmd_buf[at_cmd_len] = '\0';
	DBGPRINT(RT_DEBUG_INFO,("AT command: %s , len=%d \n",cmd_buf, at_cmd_len));
	
	/* The current process is not encough*/
	/* need improve for the command type and paramters parsing */


	if(!memcmp(cmd_buf,AT_CMD_NETMODE,strlen(AT_CMD_NETMODE)))
	{
		ret_code = IoT_exec_AT_cmd_netmode(cmd_buf, at_cmd_len);
	}
	/* Format:  AT#Ver+enter*/
	else if(!memcmp(cmd_buf,AT_CMD_VER,strlen(AT_CMD_VER)))
	{
		ret_code = IoT_exec_AT_cmd_ver(cmd_buf);
	}
	/* Format:  AT#Reboot+enter*/
	else if(!memcmp(cmd_buf,AT_CMD_REBOOT,strlen(AT_CMD_REBOOT)))
	{
		IoT_exec_AT_cmd_reboot();
	}
	/* Format:  AT#Channel -s 6+enter */
	else if(!memcmp(cmd_buf,AT_CMD_CHANNEL,strlen(AT_CMD_CHANNEL)))
	{
		cTypeLen = strlen(AT_CMD_CHANNEL);
		cmd_buf += cTypeLen;
		
		at_cmd_len -= cTypeLen;
		if(at_cmd_len > 0)
		{
			ret_code = IoT_exec_AT_cmd_ch_switch(cmd_buf, at_cmd_len);
		}
	}
#if (ATCMD_UART_SUPPORT == 1) && (UART_SUPPORT == 1)	
	/* Format:	AT#Uart -b 57600 -w 7 -p 1 -s 1 +enter*/
	else if(!memcmp(cmd_buf,AT_CMD_UART,strlen(AT_CMD_UART)))
	{
		ret_code = IoT_exec_AT_cmd_uart(cmd_buf, at_cmd_len);
	}
	
#endif

#if (ATCMD_TCPIP_SUPPORT == 1) && (CFG_SUPPORT_TCPIP == 1)
	/* Format:	AT#Tcp_Connect -t 6 -a 192.168.1.131 -p 1234 +enter*/
	else if(!memcmp(cmd_buf,AT_CMD_TCPCONNECT,strlen(AT_CMD_TCPCONNECT)))
	{

		IoT_Tcpip_AT_Connect(cmd_buf, at_cmd_len);
	}
	/* Format:	AT#Tcp_Send -s 1 -d test data +enter*/
	else if(!memcmp(cmd_buf,AT_CMD_TCPSEND,strlen(AT_CMD_TCPSEND)))
	{
		IoT_Tcpip_AT_Send(cmd_buf, at_cmd_len);
	}
	/* Format:	AT#Tcp_Listen -p 7682 +enter*/
	else if(!memcmp(cmd_buf,AT_CMD_TCPLISTEN,strlen(AT_CMD_TCPLISTEN)))
	{
		IoT_Tcpip_AT_Listen(cmd_buf, at_cmd_len);
	}
	/* Format:	AT#Tcp_Disconnect -s 0 +enter*/
	else if(!memcmp(cmd_buf,AT_CMD_TCPDISCONNECT,strlen(AT_CMD_TCPDISCONNECT)))
	{
		IoT_Tcpip_AT_Disconnect(cmd_buf, at_cmd_len);
	}

#endif


	return ret_code;
}


INT16 IoT_detect_ATcommand(PCHAR command_buffer)
{
	CHAR ch;
	INT32 read = -1;
	INT16 ret_len;
	
	if (UART_LSROverErr() != 0)
			return -1;
	
	while(1)
	{
		read = UART_GetChar();

		if(read == -1)
			return -1;

		ch = (CHAR) read;
		
		if(state != SYNC_STATE_3)
		{
			if(ch == 'A'|| ch == 'a')
			{
				state = SYNC_STATE_1;
			}
			else if(ch == 'T' || ch == 't')
			{
				if(state == SYNC_STATE_1)
					state = SYNC_STATE_2;
				else
					state = SYNC_STATE_0;
			}
			else if(ch == '#')
			{
				if(state == SYNC_STATE_2)
				{
					state = SYNC_STATE_3;
					cmd_len = 0;
					continue;
				}
				else
					state = SYNC_STATE_0;
			}
			else
			{
				state = SYNC_STATE_0;
			}
		}
			
		else if(state == SYNC_STATE_3)
		{
			if(ch == '\n')
			{
				state = SYNC_STATE_0;
				ret_len = cmd_len-1;   /*not include '\n'*/
				cmd_len = 0;
				return ret_len;
			}
			else
			{
				command_buffer[cmd_len] = ch;
				cmd_len++;
				if(cmd_len >= AT_CMD_MAX_LEN)
				{
					state = SYNC_STATE_0;
					cmd_len = 0;

					return -2;
				}
			}
		}
	}
		
}


VOID ATcommandHandler(VOID)
{
	INT16 at_cmd_len;
	INT16 ret_code;
	PCHAR pCmdBuf = NULL;

	pCmdBuf = (PCHAR)malloc(AT_CMD_MAX_LEN);

	if(pCmdBuf == NULL)
		return;
	
	at_cmd_len = IoT_detect_ATcommand(pCmdBuf);
	
	if(at_cmd_len > 0)
	{
		ret_code = IoT_parse_ATcommand(pCmdBuf, at_cmd_len);

		if(ret_code != 0)
			DBGPRINT(RT_DEBUG_ERROR,("exec error: %d\n",ret_code));
	}
	
	else if(at_cmd_len == -2)
		DBGPRINT(RT_DEBUG_ERROR,("=>%s AT command is too long\n",__FUNCTION__));

	free(pCmdBuf);

	return;
}