#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mqtt_api.h"
#include "com_api.h"
#include "cJSON.h"

static char * _kk_data_create(const char *topic,const char *data)
{
	cJSON *root;
	char *out;
	root=cJSON_CreateObject();
	cJSON_AddStringToObject(root,"topic",topic);
	cJSON_AddStringToObject(root,"payload",data);
	out=cJSON_Print(root);	
	cJSON_Delete(root);	
	printf("[%s][%d]%s\n",__FUNCTION__,__LINE__,out);	
	return out;
	//free(out);	/* Print to text, Delete the cJSON, print it, release the string. */
	
}
void KK_Sendto_CloudData(void *data,int len)
{
	cJSON *root,*pTopic,*pData;
	char *topic = NULL;
	char *payload = NULL;
	root=cJSON_Parse((char*)data);
	if(root == NULL)
	{
		return;
	}
	pTopic = cJSON_GetObjectItem(root, "topic");
	if(pTopic == NULL)
	{
		return;
	}
	pData = cJSON_GetObjectItem(root, "payload");
	if(pData == NULL)
	{
		return;
	}
	printf("[%s][%d] topic:%s\n",__FUNCTION__,__LINE__,pTopic->valuestring);
	printf("[%s][%d] payload:%s\n",__FUNCTION__,__LINE__,pData->valuestring);	
	KK_MQTT_SendMsg(pTopic->valuestring,(const char*)pData->valuestring);
}

void KK_Sendto_DevData(const char *topic,const char *data)
{
	char *send_data = _kk_data_create(topic,data);
	
	kk_ipc_send(IPC_APP2MID, send_data, strlen(send_data)+1);
}
