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

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

//豸
#include "esp8266.h"

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

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

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


#define DEVID	"6580246"

#define APIKEY	"iUZqKW6xIjpGngggBH=66VCGzqg="


extern unsigned char esp8266_buf[128];


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

	unsigned char *dataPtr;
	
	unsigned char status = 1;
	
	UsartPrintf(USART_DEBUG, "OneNet_DevLink\r\n"
                        "DEVID: %s,     APIKEY: %s\r\n"
                        , DEVID, APIKEY);

	if(EDP_PacketConnect1(DEVID, APIKEY, 256, &edpPacket) == 0)		//devid  apikeyװЭ
	{
		ESP8266_SendData(edpPacket._data, edpPacket._len);			//ϴƽ̨
		
		dataPtr = ESP8266_GetIPD(250);								//ȴƽ̨Ӧ
		if(dataPtr != NULL)
		{
			if(EDP_UnPacketRecv(dataPtr) == CONNRESP)
			{
				switch(EDP_UnPacketConnectRsp(dataPtr))
				{
					case 0:UsartPrintf(USART_DEBUG, "Tips:	ӳɹ\r\n");status = 0;break;
					
					case 1:UsartPrintf(USART_DEBUG, "WARN:	ʧܣЭ\r\n");break;
					case 2:UsartPrintf(USART_DEBUG, "WARN:	ʧܣ豸IDȨʧ\r\n");break;
					case 3:UsartPrintf(USART_DEBUG, "WARN:	ʧܣʧ\r\n");break;
					case 4:UsartPrintf(USART_DEBUG, "WARN:	ʧܣûIDȨʧ\r\n");break;
					case 5:UsartPrintf(USART_DEBUG, "WARN:	ʧܣδȨ\r\n");break;
					case 6:UsartPrintf(USART_DEBUG, "WARN:	ʧܣȨЧ\r\n");break;
					case 7:UsartPrintf(USART_DEBUG, "WARN:	ʧܣδ\r\n");break;
					case 8:UsartPrintf(USART_DEBUG, "WARN:	ʧܣ豸ѱ\r\n");break;
					case 9:UsartPrintf(USART_DEBUG, "WARN:	ʧܣظ\r\n");break;
					
					default:UsartPrintf(USART_DEBUG, "ERR:	ʧܣδ֪\r\n");break;
				}
			}
		}
		
		EDP_DeleteBuffer(&edpPacket);								//ɾ
	}
	else
		UsartPrintf(USART_DEBUG, "WARN:	EDP_PacketConnect Failed\r\n");
	
	return status;
	
}

//==========================================================
//	ƣ	OneNet_PushData
//
//	ܣ	PUSHDATA
//
//	ڲ	dst_devid豸devid
//				data
//				data_lenݳ
//
//	ز	0-ͳɹ	1-ʧ
//
//	˵		豸豸֮ͨ
//==========================================================
_Bool OneNet_PushData(const char* dst_devid, const char* data, unsigned int data_len)
{
	
	EDP_PACKET_STRUCTURE edpPacket = {NULL, 0, 0, 0};							//Э
	
	if(EDP_PacketPushData(dst_devid, data, data_len, &edpPacket) == 0)
	{
		UsartPrintf(USART_DEBUG, "Tips:	OneNet_PushData\r\n");
		
		ESP8266_SendData(edpPacket._data, edpPacket._len);						//ϴƽ̨
		
		EDP_DeleteBuffer(&edpPacket);											//ɾ
	}
	else
		UsartPrintf(USART_DEBUG, "WARN:	OneNet_PushData Failed\r\n");
	
	return 0;

}

//==========================================================
//	ƣ	OneNet_RevPro
//
//	ܣ	ƽ̨ݼ
//
//	ڲ	dataPtrƽ̨ص
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_RevPro(unsigned char *cmd)
{
	
	char *cmdid_devid = NULL;
	char *req = NULL;
	unsigned int req_len = 0;
	unsigned char type = 0;
	
	short result = 0;

	char *dataPtr = NULL;
	char numBuf[10];
	int num = 0;
	
	type = EDP_UnPacketRecv(cmd);
	switch(type)										//жpushdata·
	{
		case PUSHDATA:									//pushdata
			
			result = EDP_UnPacketPushData(cmd, &cmdid_devid, &req, &req_len);
		
			if(result == 0)
				UsartPrintf(USART_DEBUG, "src_devid: %s, req: %s, req_len: %d\r\n", cmdid_devid, req, req_len);
			
		break;
			
		default:
			result = -1;
		break;
	}
	
	ESP8266_Clear();									//ջ
	
	if(result == -1)
		return;
	
	dataPtr = strchr(req, '}');							//'}'
	
	if(dataPtr != NULL)									//ҵ
	{
		dataPtr++;
		
		while(*dataPtr >= '0' && *dataPtr <= '9')		//жǷ·
		{
			numBuf[num++] = *dataPtr++;
		}
		
		num = atoi((const char *)numBuf);				//תΪֵʽ
	}
	
	if(type == PUSHDATA && result == 0)					//pushdata  ɹ
	{
		EDP_FreeBuffer(cmdid_devid);					//ͷڴ
		EDP_FreeBuffer(req);
	}

}
