/**
	************************************************************
	************************************************************
	************************************************************
	*	ļ 	sample.c
	*
	*	ߣ 		ż
	*
	*	ڣ 		2017-06-14
	*
	*	汾 		V1.0
	*
	*	˵ 		ļ룬ֻSDKʹõʾ
	*
	*	޸ļ¼	
	************************************************************
	************************************************************
	************************************************************
**/


#include "mqttkit.h"


unsigned char dataMem[128];		//ȫ鷽ʽ


//==========================================================
//	ƣ	OneNet_DevLink
//
//	ܣ	onenet
//
//	ڲ	devid豸devid
//				proidƷID
//				auth_key豸masterKeyapiKey豸ȨϢ
//
//	ز	
//
//	˵		onenetƽ̨ӣɹoneNetInfo.netWork״̬־
//==========================================================
void OneNet_DevLink(const char* devid, const char *proid, const char* auth_info)
{
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};
	
	edpPacket._data = dataMem;				//ȫ鷽ʽ
	edpPacket._size = sizeof(dataMem);		//ȫ鷽ʽ
	
//---------------------------------------------һ---------------------------------------------
	if(MQTT_PacketConnect(proid, auth_info, devid, 256, 0, MQTT_QOS_LEVEL0, NULL, NULL, 0, &mqttPacket) == 0)
	{
//--------------------------------------------------------------------------------------
		NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);

//---------------------------------------------жϷ--------------------------------------
		if(MQTT_UnPacketRecv(dataPtr) == MQTT_PKT_CONNACK)
		{
//---------------------------------------------ģؽ--------------------------------------
			switch(MQTT_UnPacketConnectAck(dataPtr))
			{
				case 0:
					UsartPrintf(USART_DEBUG, "Tips:	ӳɹ\r\n");
					oneNetInfo.netWork = 1;
				break;
				
				case 1:UsartPrintf(USART_DEBUG, "WARN:	ʧܣЭ\r\n");break;
				case 2:UsartPrintf(USART_DEBUG, "WARN:	ʧܣǷclientid\r\n");break;
				case 3:UsartPrintf(USART_DEBUG, "WARN:	ʧܣʧ\r\n");break;
				case 4:UsartPrintf(USART_DEBUG, "WARN:	ʧܣû\r\n");break;
				case 5:UsartPrintf(USART_DEBUG, "WARN:	ʧܣǷ(tokenǷ)\r\n");break;
				
				default:UsartPrintf(USART_DEBUG, "ERR:	ʧܣδ֪\r\n");break;
			}
		}
		
//---------------------------------------------壺ɾ---------------------------------------------
		MQTT_DeleteBuffer(&mqttPacket);
	}
	else
		UsartPrintf(USART_DEBUG, "WARN:	MQTT_PacketConnect Failed\r\n");
	
}

//==========================================================
//	ƣ	OneNet_DisConnect
//
//	ܣ	ƽ̨Ͽ
//
//	ڲ	
//
//	ز	0-ɹ		1-ʧ
//
//	˵		
//==========================================================
_Bool OneNet_DisConnect(void)
{

	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};
	unsigned char dataMem1[8];				//ֲ鷽ʽ
	
	edpPacket._data = dataMem;				//ֲ鷽ʽ
	edpPacket._size = sizeof(dataMem);		//ֲ鷽ʽ
	
//---------------------------------------------һ---------------------------------------------
	if(MQTT_PacketDisConnect(&mqttPacket) == 0)
	{
//--------------------------------------------------------------------------------------
		NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);
		
//---------------------------------------------ɾ---------------------------------------------
		MQTT_DeleteBuffer(&mqttPacket);
	}
	
	return 0;

}

//==========================================================
//	ƣ	OneNet_SendData
//
//	ܣ	ϴݵƽ̨
//
//	ڲ	typeݵĸʽ
//				devid豸ID
//				apikey豸apikey
//				streamArray
//				streamArrayNum
//
//	ز	0-ɹ		1-ʧ
//
//	˵		
//==========================================================
_Bool OneNet_SendData(FORMAT_TYPE type, char *devid, char *apikey, DATA_STREAM *streamArray, unsigned short streamArrayCnt)
{
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};
	
	_Bool status = 0;
	short body_len = 0;
	
	UsartPrintf(USART_DEBUG, "Tips:	OneNet_SendData-MQTT_TYPE%d\r\n", type);
	
//---------------------------------------------һ---------------------------------------------
	body_len = DSTREAM_GetDataStream_Body_Measure(type, streamArray, streamArrayCnt, 0);
	if(body_len)
	{
//---------------------------------------------дЭͷ-------------------------------------------------
		if(MQTT_PacketSaveData(devid, body_len, NULL, (uint8)type, &mqttPacket) == 0)
		{
//----------------------------------------------------------------------------------------------------
			body_len = DSTREAM_GetDataStream_Body(type, streamArray, streamArrayCnt, mqttPacket._data, mqttPacket._size, mqttPacket._len);
			
			if(body_len)
			{
				mqttPacket._len += body_len;
				UsartPrintf(USART_DEBUG, "Send %d Bytes\r\n", mqttPacket._len);
//---------------------------------------------ģ---------------------------------------------------
				NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);
			}
			else
				UsartPrintf(USART_DEBUG, "WARN:	DSTREAM_GetDataStream_Body Failed\r\n");
			
//---------------------------------------------壺ɾ-------------------------------------------------------
			MQTT_DeleteBuffer(&mqttPacket);
		}
		else
			UsartPrintf(USART_DEBUG, "WARN:	MQTT_NewBuffer Failed\r\n");
	}
	else
		status = 1;
	
	return status;
	
}

//==========================================================
//	ƣ	OneNet_HeartBeat
//
//	ܣ	
//
//	ڲ	
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_HeartBeat(void)
{
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};

	unsigned char sCount = 3;
	
//---------------------------------------------һ---------------------------------------------
	if(MQTT_PacketPing(&mqttPacket))
		return;
	
	while(sCount--)
	{
//--------------------------------------------------------------------------------------
		NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);

//----------------------------------------------------------------------------------
		if(MQTT_UnPacketRecv(dataPtr) == MQTT_PKT_PINGRESP)
		{
			UsartPrintf(USART_DEBUG, "Tips:	HeartBeat OK\r\n");
			
			break;
		}
		else
		{
			UsartPrintf(USART_DEBUG, "Check Device\r\n");
		}
		
		RTOS_TimeDly(2);
	}
	
//---------------------------------------------ģɾ---------------------------------------------
	MQTT_DeleteBuffer(&mqttPacket);

}

//==========================================================
//	ƣ	OneNet_Publish
//
//	ܣ	Ϣ
//
//	ڲ	topic
//				msgϢ
//
//	ز	0-ɹ	1-ʧ
//
//	˵		
//==========================================================
_Bool OneNet_Publish(const char *topic, const char *msg)
{

	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};
	
	UsartPrintf(USART_DEBUG, "Publish Topic: %s, Msg: %s\r\n", topic, msg);
	
//---------------------------------------------һ---------------------------------------------
	if(MQTT_PacketPublish(MQTT_PUBLISH_ID, topic, msg, strlen(msg), MQTT_QOS_LEVEL2, 0, 1, &mqttPacket) == 0)
	{
//--------------------------------------------------------------------------------------
		NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);
		
//---------------------------------------------ɾ---------------------------------------------
		MQTT_DeleteBuffer(&mqttPacket);
	}
	
	return 0;

}

//==========================================================
//	ƣ	OneNet_Subscribe
//
//	ܣ	
//
//	ڲ	topicsĵtopic
//				topic_cnttopic
//
//	ز	0-ɹ	1-ʧ
//
//	˵		
//==========================================================
_Bool OneNet_Subscribe(const char *topics[], unsigned char topic_cnt)
{
	
	unsigned char i = 0;
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};							//Э
	
	for(; i < topic_cnt; i++)
		UsartPrintf(USART_DEBUG, "Subscribe Topic: %s\r\n", topics[i]);
	
//---------------------------------------------һ---------------------------------------------
	if(MQTT_PacketSubscribe(MQTT_SUBSCRIBE_ID, MQTT_QOS_LEVEL2, topics, topic_cnt, &mqttPacket) == 0)
	{
//--------------------------------------------------------------------------------------
		NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);					//ƽ̨Ͷ
		
//---------------------------------------------ɾ---------------------------------------------
		MQTT_DeleteBuffer(&mqttPacket);											//ɾ
	}
	
	return 0;

}

//==========================================================
//	ƣ	OneNet_UnSubscribe
//
//	ܣ	ȡ
//
//	ڲ	topicsĵtopic
//				topic_cnttopic
//
//	ز	0-ɹ	1-ʧ
//
//	˵		
//==========================================================
_Bool OneNet_UnSubscribe(const char *topics[], unsigned char topic_cnt)
{
	
	unsigned char i = 0;
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};							//Э
	
	for(; i < topic_cnt; i++)
		UsartPrintf(USART_DEBUG, "UnSubscribe Topic: %s\r\n", topics[i]);
	
//---------------------------------------------һ---------------------------------------------
	if(MQTT_PacketUnSubscribe(MQTT_UNSUBSCRIBE_ID, topics, topic_cnt, &mqttPacket) == 0)
	{
//--------------------------------------------------------------------------------------
		NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);					//ƽ̨ȡ
		
//---------------------------------------------ɾ---------------------------------------------
		MQTT_DeleteBuffer(&mqttPacket);											//ɾ
	}
	
	return 0;

}

//==========================================================
//	ƣ	OneNet_RevPro
//
//	ܣ	ƽ̨ݼ
//
//	ڲ	dataPtrƽ̨ص
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_RevPro(unsigned char *cmd)
{
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};
	
	char *req_payload = NULL;
	char *cmdid_topic = NULL;
	unsigned char type = 0;
	unsigned char qos = 0;
	static unsigned short pkt_id = 0;
	
	short result = -1;
	
//---------------------------------------------һȡ---------------------------------------------
	type = MQTT_UnPacketRecv(cmd);
	switch(type)
	{
//---------------------------------------------ú-------------------------------------------------
		case MQTT_PKT_CMD:															//·
			
			result = MQTT_UnPacketCmd(cmd, &cmdid_topic, &req_payload);				//topicϢ
			if(result == 0)
			{
				UsartPrintf(USART_DEBUG, "cmdid: %s, req: %s\r\n", cmdid_topic, req_payload);
				
				if(MQTT_PacketCmdResp(cmdid_topic, req_payload, &mqttPacket) == 0)	//ظ
				{
					UsartPrintf(USART_DEBUG, "Tips:	Send CmdResp\r\n");
					
					NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);			//ظ
					MQTT_DeleteBuffer(&mqttPacket);									//ɾ
				}
			}
		
		break;
			
		case MQTT_PKT_PUBLISH:														//յPublishϢ
		
			result = MQTT_UnPacketPublish(cmd, &cmdid_topic, &req_payload, &qos, &pkt_id);
			if(result == 0)
			{
				UsartPrintf(USART_DEBUG, "topic: %s\r\npayload: %s\r\n", cmdid_topic, req_payload);
				
				switch(qos)
				{
					case 1:															//յpublishqosΪ1豸ҪظAck
					
						if(MQTT_PacketPublishAck(pkt_id, &mqttPacket) == 0)
						{
							UsartPrintf(USART_DEBUG, "Tips:	Send PublishAck\r\n");
							NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);
							MQTT_DeleteBuffer(&mqttPacket);
						}
					
					break;
					
					case 2:															//յpublishqosΪ2豸ȻظRec
																					//ƽ̨ظRel豸ٻظComp
						if(MQTT_PacketPublishRec(pkt_id, &mqttPacket) == 0)
						{
							UsartPrintf(USART_DEBUG, "Tips:	Send PublishRec\r\n");
							NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);
							MQTT_DeleteBuffer(&mqttPacket);
						}
					
					break;
					
					default:
						break;
				}
			}
		
		break;
			
		case MQTT_PKT_PUBACK:														//PublishϢƽ̨ظAck
		
			if(MQTT_UnPacketPublishAck(cmd) == 0)
				UsartPrintf(USART_DEBUG, "Tips:	MQTT Publish Send OK\r\n");
			
		break;
			
		case MQTT_PKT_PUBREC:														//PublishϢƽ̨ظRec豸ظRelϢ
		
			if(MQTT_UnPacketPublishRec(cmd) == 0)
			{
				UsartPrintf(USART_DEBUG, "Tips:	Rev PublishRec\r\n");
				if(MQTT_PacketPublishRel(MQTT_PUBLISH_ID, &mqttPacket) == 0)
				{
					UsartPrintf(USART_DEBUG, "Tips:	Send PublishRel\r\n");
					NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);
					MQTT_DeleteBuffer(&mqttPacket);
				}
			}
		
		break;
			
		case MQTT_PKT_PUBREL:														//յPublishϢ豸ظRecƽ̨ظRel豸ٻظComp
			
			if(MQTT_UnPacketPublishRel(cmd, pkt_id) == 0)
			{
				UsartPrintf(USART_DEBUG, "Tips:	Rev PublishRel\r\n");
				if(MQTT_PacketPublishComp(MQTT_PUBLISH_ID, &mqttPacket) == 0)
				{
					UsartPrintf(USART_DEBUG, "Tips:	Send PublishComp\r\n");
					NET_DEVICE_SendData(mqttPacket._data, mqttPacket._len);
					MQTT_DeleteBuffer(&mqttPacket);
				}
			}
		
		break;
		
		case MQTT_PKT_PUBCOMP:														//PublishϢƽ̨Rec豸ظRelƽ̨ٷصComp
		
			if(MQTT_UnPacketPublishComp(cmd) == 0)
			{
				UsartPrintf(USART_DEBUG, "Tips:	Rev PublishComp\r\n");
			}
		
		break;
			
		case MQTT_PKT_SUBACK:														//SubscribeϢAck
		
			if(MQTT_UnPacketSubscribe(cmd) == 0)
				UsartPrintf(USART_DEBUG, "Tips:	MQTT Subscribe OK\r\n");
			else
				UsartPrintf(USART_DEBUG, "Tips:	MQTT Subscribe Err\r\n");
		
		break;
			
		case MQTT_PKT_UNSUBACK:														//UnSubscribeϢAck
		
			if(MQTT_UnPacketUnSubscribe(cmd) == 0)
				UsartPrintf(USART_DEBUG, "Tips:	MQTT UnSubscribe OK\r\n");
			else
				UsartPrintf(USART_DEBUG, "Tips:	MQTT UnSubscribe Err\r\n");
		
		break;
	}
	
//------------------------------------------------------------------------------------------
	if(req)
	{
		
	}
//---------------------------------------------ģͷڴ---------------------------------------------
	if(type == MQTT_PKT_CMD || type == MQTT_PKT_PUBLISH)
	{
		MQTT_FreeBuffer(cmdid_topic);
		MQTT_FreeBuffer(req_payload);
	}

}
