/**
	************************************************************
	************************************************************
	************************************************************
	*	ļ 	onenet.c
	*
	*	ߣ 		ż
	*
	*	ڣ 		2017-05-08
	*
	*	汾 		V1.1
	*
	*	˵ 		onenetƽ̨ݽӿڲ
	*
	*	޸ļ¼	V1.0Эװж϶ͬһļҲͬЭӿڲͬ
	*				V1.1ṩͳһӿڹӦòʹãݲͬЭļװЭصݡ
	************************************************************
	************************************************************
	************************************************************
**/

//Ƭͷļ
#include "stm32f10x.h"

//豸
#include "m6312.h"

//Эļ
#include "onenet.h"
#include "tcpkit.h"

//Ӳ
#include "usart.h"
#include "delay.h"
#include "adxl362.h"

//C
#include <string.h>
#include <stdio.h>


#define PROID		"187421"

#define AUTH_INFO	"test"

#define LUA_NAME	"tcp362"


//==========================================================
//	ƣ	OneNet_DevLink
//
//	ܣ	onenet
//
//	ڲ	
//
//	ز	1-ɹ	0-ʧ
//
//	˵		onenetƽ̨
//==========================================================
_Bool OneNet_DevLink(void)
{
	
	TCP_PACKET_STRUCTURE tcpPacket = {NULL, 0, 0, 0};				//Э

	unsigned char *dataPtr;
	
	unsigned char status = 1;
	
	UsartPrintf(USART_DEBUG, "OneNET_DevLink\r\n"
							"PROID: %s,	AUIF: %s,	LUA:%s\r\n"
                        , PROID, AUTH_INFO, LUA_NAME);

	if(TCP_PacketConnect(PROID, AUTH_INFO, LUA_NAME, &tcpPacket) == 0)
	{
		M6312_SendData(tcpPacket._data, tcpPacket._len);			//ϴƽ̨
		
		dataPtr = M6312_GetIPD(250);								//ȴƽ̨Ӧ
		if(dataPtr != NULL)
		{
			if(TCP_UnPacketRecv(dataPtr) == TCP_CONNECT)
			{
				UsartPrintf(USART_DEBUG, "Tips:	ӳɹ\r\n");
				status = 0;
			}
		}
		
		TCP_DeleteBuffer(&tcpPacket);								//ɾ
	}
	else
		UsartPrintf(USART_DEBUG, "WARN:	TCP_PacketConnect Failed\r\n");
	
	return status;
	
}

void OneNet_FillBuf(unsigned char *buf, unsigned char offset)
{
	
	unsigned int temp = 0;
	unsigned char *data_ptr = buf + offset;
	
	temp = adxl362Info.x * 100;
	*data_ptr++ = temp >> 24;
	*data_ptr++ = temp >> 16;
	*data_ptr++ = temp >> 8;
	*data_ptr++ = temp  & 0x00FF;
	
	temp = adxl362Info.y * 100;
	*data_ptr++ = temp >> 24;
	*data_ptr++ = temp >> 16;
	*data_ptr++ = temp >> 8;
	*data_ptr++ = temp  & 0x00FF;
	
	temp = adxl362Info.z * 100;
	*data_ptr++ = temp >> 24;
	*data_ptr++ = temp >> 16;
	*data_ptr++ = temp >> 8;
	*data_ptr++ = temp  & 0x00FF;

}

//==========================================================
//	ƣ	OneNet_SendData
//
//	ܣ	ϴݵƽ̨
//
//	ڲ	typeݵĸʽ
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_SendData(void)
{
	
	TCP_PACKET_STRUCTURE tcpPacket = {NULL, 0, 0, 0};											//Э
	
	UsartPrintf(USART_DEBUG, "Tips:	OneNet_SendData-TCP\r\n");
	
	if(TCP_PacketData(12, &tcpPacket) == 0)
	{
		OneNet_FillBuf(tcpPacket._data, 3);
		tcpPacket._len = 17;
		
		M6312_SendData(tcpPacket._data, tcpPacket._len);										//ϴݵƽ̨
		UsartPrintf(USART_DEBUG, "Send %d Bytes\r\n", tcpPacket._len);
		
		TCP_DeleteBuffer(&tcpPacket);															//ɾ
	}
	else
		UsartPrintf(USART_DEBUG, "WARN:	EDP_NewBuffer Failed\r\n");
	
}

//==========================================================
//	ƣ	OneNet_RevPro
//
//	ܣ	ƽ̨ݼ
//
//	ڲ	dataPtrƽ̨ص
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_RevPro(unsigned char *cmd)
{
	
	M6312_Clear();										//ջ

}
