#include<stdio.h>
#include"com_api.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include "cJSON.h"
#include "kk_product.h"



void mid_cb(void* data, int len){
	if (data != NULL){
		printf("app2mid_cb: %s RECEIVED \r\n", data); 
        char *out;cJSON *json, *topic, *payload;

    	json=cJSON_Parse(data);
    	if (!json) {
            printf("Error before: [%s]\n","cJSON_Parse");
        }
    	else
    	{
            topic = cJSON_GetObjectItem(json, "topic");
            payload = cJSON_GetObjectItem(json, "payload");
            kk_tsl_service_property_set(topic->valuestring, payload->valuestring,strlen(payload->valuestring),NULL);
	    }
        kk_ipc_send(IPC_MID2PLAT, data, len);
	}
}

void mid2p_cb(void* data, int len){
	if (data != NULL){
		printf("mid2p_cb: %s RECEIVED \r\n", data); 
        kk_ipc_send(IPC_MID2APP, data, len);
	}
}



#define MYPORT  5555
#define BUFFER_SIZE 1024

void test_tcp(){

    ///定义sockfd
    int sock_cli = socket(AF_INET,SOCK_STREAM, 0);
 
    ///定义sockaddr_in
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(MYPORT);  ///服务器端口
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  ///服务器ip
 
    ///连接服务器，成功返回0，错误返回-1
    if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("connect");
        exit(1);
    }
 
    char sendbuf[BUFFER_SIZE]={0};
    char recvbuf[BUFFER_SIZE];
    //printf("midd: %s send \r\n", sendbuf); 
    //send(sock_cli, sendbuf, strlen(sendbuf),0);
    while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
    {
        printf("midd: %s send \r\n", sendbuf); 
        send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送
        if(strcmp(sendbuf,"exit\n")==0)
            break;
        //printf("midd: %s send \r\n", sendbuf); 
        recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收
        fputs(recvbuf, stdout);
 
        memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));
    }
 
    close(sock_cli);
    
}

time_t getSysTime(){
    time_t t;  
    t = time(NULL);  
    return t;

}
static int kk_set_product_info(void)
{
	HAL_SetProduct_Type(PRODUCT_TPYE);
	HAL_SetProduct_Code(PRODUCT_CODE);	
	kk_set_tsl_by_productKey(PRODUCT_TPYE,"gateway.json");
	return 0;
}

int main(const int argc, const char **argv)
{
		kk_set_product_info();
        kk_tsl_api_init();
        kk_ipc_init(IPC_MID2APP, mid_cb);
        kk_ipc_init(IPC_MID2PLAT, mid2p_cb);
		dm_mgr_init();
		
		
        //DB_Init();
        //test_tcp();
        char buf[100] = {0};
		for (;;) {
                sleep(5);
                /*memset(buf, 0, 100);
                sprintf(buf , "send data app time = %d\n", getSysTime());
                printf("M2A: %s", buf);
                kk_ipc_send(IPC_MID2APP, buf, strlen(buf)+1);
                memset(buf, 0, 100);
                sprintf(buf, "send data to plat time = %d\n", getSysTime());
                printf("M2P: %s", buf);
                kk_ipc_send(IPC_MID2PLAT, buf, strlen(buf) + 1);*/
        }
}

