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


#include <stdio.h>
#include "string.h"
#include "types.h"
#include "iot_api.h"
#include "config.h"
#if 0 //CFG_SUPPORT_TCPIP
#include "uip.h"
#endif
#include "flash_map.h"

/******************************************************************************
* MODULE NAME:     iot_parse.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:
*
******************************************************************************/


/*

  UINT_32 Magic;					  //protocol magic number
  UINT_8 ReceiveMAC[MAC_ADDR_LEN];  //receive mac address
  UINT_8 SendMAC[MAC_ADDR_LEN];	  //sender mac address
  UINT_32 SessionID;				  //reserved
  UINT_16 Sequence;				  //sequence number
  UINT_16 Flag:4; 				  //reserved
  UINT_16 Type:4; 				  //command type. 0: management command 1: function command
  UINT_16 SubType:8;				  //subtype
  
	
	  UINT_16 Type;
	  UINT_16 Length;
	  DataHeader;
	  
		
		  UINT_32 GPIO_Bitmap;
		  
			0x50438176
			
*/

/*
UCHAR iot_packet_set_gpio[] = {0x50,0x43,0x81,0x76,\
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,\
0x2,0x2,0x2,0x2,0x2,0x2,\
0x3,0x3,0x3,0x3,\
0x4,0x4,\
0x10,0x01,\
0x3,0x0,\
0x64,0x0,\
0x1C,0x0,0x0,0x0\	   

  };
  
	
	  UCHAR iot_packet_get_gpio[] = {0x50,0x43,0x81,0x76,\
	  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,\
	  0x2,0x2,0x2,0x2,0x2,0x2,\
	  0x3,0x3,0x3,0x3,\
	  0x4,0x4,\
	  0x10,0x03,\
	  0x3,0x0,\
	  0x64,0x0,\
	  0x1C,0x0,0x0,0x0\
	  
		
		  };
		  
*/
/*
UINT_16 Length;
UINT_16 RedLevel;
UINT_16 GreenLevel;
UINT_16 BlueLevel;
*/

/*
UCHAR iot_packet2[] = {0x50,0x43,0x81,0x76,\
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,\
0x2,0x2,0x2,0x2,0x2,0x2,\
0x3,0x3,0x3,0x3,\
0x4,0x4,\
0x10,0x09,\
0x4,0x0,\
0x64,0x0,\
0x3,0x0,\
0x4,0x0,\
0x5,0x0\
};

*/

//if(pAd.Mlme.DataEnable == 1)
//{
//printf("IoT send data6\n");

//IoT_process_app_packet(iot_packet2);
//}


//1024+28+2

#define TCPUDP_SRC_PORT		(34932)
#define TCPUDP_DST_PORT		(8176)
#define IOT_BUFFER_LEN 		(256)
UCHAR src_addr[]={192,168,1,10};
UCHAR dest_addr[]={255,255,255,255};


//UCHAR payload[] = "Test";
//UINT16 payload_len = sizeof(payload);
//UCHAR packet[] = {0x45,0x00,0x00,0x34,0x1F,0xCF,0x40,0x00,0x40,0x06,0xDA,0xF6,0xBC,0xFD,0xDA,0x1E,0x6C,0x39};
//IoT_build_send_udp_packet(0, src_addr, dest_addr, SrcPort, DstPort, payload,payload_len);

UCHAR VendorName[FLASH_USR_CFG_VENDOR_NAME_LEN]		=	"Mediatek";
UCHAR ProductName[FLASH_USR_CFG_PRODUCT_NAME_LEN]	=	"MT7681";
UCHAR ProductType[FLASH_USR_CFG_PRODUCT_TYPE_LEN]	=	"IoT 1";

UINT_16 previous_sequence;

//t_IoTHardwareRresource mt7681_HardwareRresource = {00011100B,00000000B,00001110B,7,1,4};
//t_IoTHardwareRresource mt7681_HardwareRresource = {0x1C,    0x00,   0x0E,    7,1,4};
//t_IoTHardwareRresource mt7681_HardwareRresource = {00011100B,00000001B,00001100B,7,1,4};
t_IoTHardwareRresource mt7681_HardwareRresource = {0x1C, 0x01, 0x0C, 0x7, 0x1, 0x4};
t_IoTHardwareStatus mt7681_HardwareStatus;


INT32 IoT_process_app_function_packet(
						struct  t_DataHeader* DataHeader, 
						UINT8 FunctionType, 
						struct t_IoTPacketInfo  *PacketInfo)
{
	UCHAR iot_buffer[IOT_BUFFER_LEN]={0};
	PUCHAR Data = (PUCHAR)(DataHeader) + sizeof(struct t_DataHeader); 
	INT32 i;
	bool Need_resp = TRUE;
	
	UCHAR * Data_out = (UCHAR *)(iot_buffer+sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader));
	UINT16 payload_len =0;
	
	switch (FunctionType)
	{
		case GPIO_SET_REQUEST :
			DBGPRINT(RT_DEBUG_TRACE,("GPIO_SET_REQUEST \n"));
			if(DataHeader->Type == GPIO_INFORMATION)
			{
				struct t_GPIO_Information *GpioData = (struct t_GPIO_Information *)Data;
				UINT_32 GPIO_Value = GpioData->GPIO_Value;
				
				if(PacketInfo->Sequence != previous_sequence)
				{
					for(i=0; i < mt7681_HardwareRresource.GPIO_Count; i++)
					{
						//DBGPRINT(RT_DEBUG_TRACE,("i = %d\n",i));
						if(!((mt7681_HardwareRresource.GPIO_Rresource)&(1<<i)))
							continue;
						
						if((GPIO_Value)&(1<<(i)))
						{
							IoT_gpio_output(i,1);
							//DBGPRINT(RT_DEBUG_TRACE,("iot gpio ok high: %d \n", i));
						}
						else
						{
							IoT_gpio_output(i,0);
							//DBGPRINT(RT_DEBUG_TRACE,("iot gpio ok low: %d \n", i));
						}
					}
				}
				
				IoT_build_app_response_header(iot_buffer, FUNCTION, GPIO_SET_RESPONSE, STATUS, sizeof(struct t_Status), PacketInfo);	
				
				struct t_Status *Status_out = (struct t_Status *)	Data_out;				
				Status_out->StatusCode = 0;								
			}
			
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_Status);	
			break;
			
		case PWM_SET_REQUEST:
			DBGPRINT(RT_DEBUG_TRACE,("PWM_SET_REQUEST\n"));
			
			if(DataHeader->Type == PWM_INFORMATION)
			{
				struct t_PWM_Information *LedData = (struct t_PWM_Information *)Data;
				IoT_led_pwm(1, LedData->RedLevel);
				IoT_led_pwm(2, LedData->GreenLevel);
				IoT_led_pwm(3, LedData->BlueLevel);
				
				mt7681_HardwareStatus.RedLevel = LedData->RedLevel;
				mt7681_HardwareStatus.GreenLevel = LedData->GreenLevel;
				mt7681_HardwareStatus.BlueLevel = LedData->BlueLevel;
				
				IoT_build_app_response_header(iot_buffer, FUNCTION, PWM_SET_RESPONSE, STATUS, sizeof(struct t_Status), PacketInfo);	
				
				struct t_Status *Status_out = (struct t_Status *)	Data_out;				
				Status_out->StatusCode = 0;				
			}
			
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_Status);	
			break;
			
		case UART_SET_REQUEST:
			DBGPRINT(RT_DEBUG_TRACE,("UART_SET_REQUEST\n"));
			
			if(DataHeader->Type == UART_INFORMATION)
			{
				struct t_UART_Information *UartData = (struct t_UART_Information *)Data;
				if(PacketInfo->Sequence != previous_sequence)
				{
					IoT_uart_output(UartData->Data, DataHeader->Length);
				}
				
				IoT_build_app_response_header(iot_buffer, FUNCTION, UART_SET_RESPONSE, STATUS, sizeof(struct t_Status), PacketInfo);	
				
				struct t_Status *Status_out = (struct t_Status *)	Data_out;		
				Status_out->StatusCode = 0;				
			}
			
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_Status);					
			break;
			
		case GPIO_GET_REQUEST:
			
			DBGPRINT(RT_DEBUG_TRACE,("GPIO_GET_REQUEST\n"));
			
			IoT_build_app_response_header(iot_buffer, FUNCTION, GPIO_GET_RESPONSE, GPIO_INFORMATION, sizeof(struct t_GPIO_Information), PacketInfo);
			
			struct t_GPIO_Information *GPIO_Information_out = (struct t_GPIO_Information *) Data_out;
			GPIO_Information_out->GPIO_List = 0;
			GPIO_Information_out->GPIO_Value = 0;			
			INT32 gpio_input = 0;			
			
			for(i=0; i < mt7681_HardwareRresource.GPIO_Count; i++)
			{
				if(!((mt7681_HardwareRresource.GPIO_Rresource)&(1<<i)))
					continue;
				
				IoT_gpio_input(i, &gpio_input);
				
				GPIO_Information_out->GPIO_List |= (0x01<<i);
				
				if(gpio_input == 1)
				{
					GPIO_Information_out->GPIO_Value |= (0x01<<i);
				}
				
				DBGPRINT(RT_DEBUG_TRACE,("gpio %d input: %d\n",i,gpio_input));
			}
			
			DBGPRINT(RT_DEBUG_TRACE,("gpio list&value %x,%x: %d\n",GPIO_Information_out->GPIO_List,GPIO_Information_out->GPIO_Value));				
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_GPIO_Information);					
			break;
			
		case PWM_GET_REQUEST:
			
			DBGPRINT(RT_DEBUG_TRACE,("PWM_GET_REQUEST\n"));	
			
			IoT_build_app_response_header(iot_buffer, FUNCTION, PWM_GET_RESPONSE, PWM_INFORMATION, sizeof(struct t_PWM_Information), PacketInfo);
			
			struct t_PWM_Information *LedData = (struct t_PWM_Information *)Data_out;
			LedData->RedLevel = mt7681_HardwareStatus.RedLevel;
			LedData->GreenLevel = mt7681_HardwareStatus.GreenLevel;
			LedData->BlueLevel = mt7681_HardwareStatus.BlueLevel;
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_PWM_Information);			
			break;
		
		case UART_GET_REQUEST:
			
			DBGPRINT(RT_DEBUG_TRACE,("UART_GET_REQUEST\n"));

			#if 1  //Disable this function for furture improve
				return 0;
			#else
			struct t_UART_Information *UartData_in = (struct t_UART_Information *)Data;
			
			IoT_build_app_response_header(iot_buffer, FUNCTION, UART_GET_RESPONSE, UART_INFORMATION, sizeof(struct t_UART_Information), PacketInfo);
			
			struct t_UART_Information *UartData_out = (struct t_UART_Information *)Data_out;
			
			if(PacketInfo->Sequence != previous_sequence)
			{
				IoT_uart_input(UartData_out->Data, UartData_in->Length);
			}

			UartData_out->Length = UartData_in->Length;
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_UART_Information);
			#endif
			
			break;
					
				
	}
	
	if(Need_resp == TRUE)
	{
#if CFG_SUPPORT_TCPIP
		uip_send(iot_buffer, payload_len);
#else
		IoT_build_send_udp_packet(src_addr, dest_addr, TCPUDP_SRC_PORT, 
							TCPUDP_DST_PORT, iot_buffer, payload_len);
#endif
	}
	
	return 0;
}

INT32 IoT_process_app_management_packet(
							struct t_DataHeader* DataHeader, 
							UINT16 ManagementType, 
							struct t_IoTPacketInfo  *PacketInfo)
{
	UCHAR iot_buffer[IOT_BUFFER_LEN]={0};
	
	//struct  t_IoTCtrlProtocolHeader * ProtocolHeader_out = (struct t_IoTCtrlProtocolHeader *)(iot_buffer);
	//struct  t_DataHeader * DataHeader_out = (struct t_DataHeader *)(iot_buffer+sizeof(struct t_IoTCtrlProtocolHeader));
	UCHAR * Data_out = (UCHAR *)(iot_buffer+sizeof(struct t_IoTCtrlProtocolHeader)
						+sizeof(struct t_DataHeader));
	struct t_ClientCapability *ClientCapability_out = (struct t_ClientCapability *) Data_out;
	UINT16 payload_len =0;
	bool Need_resp = TRUE;
	
	//PUCHAR Data= (PUCHAR)(DataHeader) + sizeof(struct t_DataHeader); 
	//DBGPRINT(RT_DEBUG_TRACE,("ProtocolHeader->Type == MANAGEMENT \n"));
	
	switch (ManagementType)
	{
		case QUERY_CAPAB_REQUEST:
			
			DBGPRINT(RT_DEBUG_TRACE,("CAPAB_REQ\n"));
			IoT_build_app_response_header(iot_buffer, MANAGEMENT, QUERY_CAPAB_RESPONSE,
				CLIENT_CAPABILITY, sizeof(struct t_ClientCapability), PacketInfo);														

			ClientCapability_out->VendorNameLen = sizeof(VendorName);
			memcpy(ClientCapability_out->VendorName,VendorName, ClientCapability_out->VendorNameLen);
			ClientCapability_out->ProductTypeLen = sizeof(ProductType);
			memcpy(ClientCapability_out->ProductType,ProductType, ClientCapability_out->ProductTypeLen);
			ClientCapability_out->ProductNameLen = sizeof(ProductName);
			memcpy(ClientCapability_out->ProductName,ProductName,ClientCapability_out->ProductNameLen);
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_ClientCapability);

			break;
		
		case CONTROL_CLIENT_OFFLINE_REQUEST:
			
			DBGPRINT(RT_DEBUG_TRACE,("OFFLINE_REQUEST\n")); 		
			
			IoT_build_app_response_header(iot_buffer, MANAGEMENT, CONTROL_CLIENT_OFFLINE_RESPONSE, STATUS, sizeof(struct t_Status), PacketInfo);
			
			struct t_Status *Status_out = (struct t_Status *) Data_out;
			Status_out->StatusCode = 0;
			
			payload_len = sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader)+sizeof(struct t_Status);
#if CFG_SUPPORT_TCPIP
			uip_send(iot_buffer, payload_len);
#else
			IoT_build_send_udp_packet(src_addr, dest_addr, TCPUDP_SRC_PORT, TCPUDP_DST_PORT, iot_buffer, payload_len);
#endif		
			IoT_Cmd_LinkDown(0);
			Need_resp = FALSE;
			
			break;
	}
//	strlen("s\n"); //To avoid crash, May be a compiler bug.;
	if(Need_resp == TRUE)
	{	
#if CFG_SUPPORT_TCPIP
#if 0
		{
			UCHAR i;
			printf("payload_len: %d\n", payload_len);
			for (i=0;i<payload_len;i++) {
				printf("0x%02x ", iot_buffer[i]);
			}
			printf("\n");
		}
#endif
		uip_send(iot_buffer, payload_len);
#else
		IoT_build_send_udp_packet(src_addr, dest_addr, TCPUDP_SRC_PORT, TCPUDP_DST_PORT, iot_buffer, payload_len);	
#endif
	}
	
	return 0;
}



INT32 IoT_process_app_packet(PUCHAR packet , UINT16 rawpacketlength)
{
	struct  t_IoTCtrlProtocolHeader * ProtocolHeader = (struct t_IoTCtrlProtocolHeader *)packet;
	struct  t_DataHeader * DataHeader = (struct  t_DataHeader *)(packet+sizeof(struct t_IoTCtrlProtocolHeader));
	struct  t_IoTPacketInfo  PacketInfo;
	
	if(rawpacketlength <= 4)
		return -1;
	
	DBGPRINT(RT_DEBUG_TRACE,("Mg:%x\n",ProtocolHeader->Magic));
	if(ProtocolHeader->Magic != IOT_MAGIC_NUMBER)
		return -2;
	
	if(MAC_ADDR_EQUAL(gCurrentAddress,ProtocolHeader->SendMAC))
	{
		DBGPRINT(RT_DEBUG_ERROR,("drop self's packet\n"));
		return -3;
	}
	
	UCHAR BROADCAST_ADDR[MAC_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
	if(!MAC_ADDR_EQUAL(gCurrentAddress, ProtocolHeader->ReceiveMAC) && !MAC_ADDR_EQUAL(BROADCAST_ADDR, ProtocolHeader->ReceiveMAC))
	{
		DBGPRINT(RT_DEBUG_ERROR,("ReceiveMAC error\n"));
		return -4;
	}
	
	PacketInfo.SessionID = ProtocolHeader->SessionID;
	PacketInfo.Sequence = ProtocolHeader->Sequence;
	
	COPY_MAC_ADDR(PacketInfo.ReceiveMAC, ProtocolHeader->ReceiveMAC);
	COPY_MAC_ADDR(PacketInfo.SendMAC, ProtocolHeader->SendMAC);
	
	DBGPRINT(RT_DEBUG_TRACE,("sendmac: %02x:%02x:%02x:%02x:%02x:%02x \n",PRINT_MAC(PacketInfo.SendMAC)));
	DBGPRINT(RT_DEBUG_TRACE,("receiveMAC: %02x:%02x:%02x:%02x:%02x:%02x \n",PRINT_MAC(PacketInfo.ReceiveMAC)));
	
	//IoT_uart_output(packet,sizeof(struct t_IoTCtrlProtocolHeader)+sizeof(struct t_DataHeader));	   

	/*no need handler the packet with the same seqence number as before*/
	if(PacketInfo.Sequence == previous_sequence)
		return 0;
	
	if(ProtocolHeader->Type == FUNCTION)
	{
		IoT_process_app_function_packet(DataHeader, ProtocolHeader->SubType, &PacketInfo);
	}
	else if(ProtocolHeader->Type == MANAGEMENT)		
	{
		IoT_process_app_management_packet(DataHeader, ProtocolHeader->SubType, &PacketInfo);
	}

	previous_sequence = PacketInfo.Sequence;
	
	return 0;
	
}