﻿#ifndef __LINKKIT_GATEWAY_COMMON_H
#define __LINKKIT_GATEWAY_COMMON_H

/***************HEADER FILES****************/
#include <string.h>
#include <syslog.h>
#include "ipc_session.h"
#include "lnxall_list.h"
#include "dy_utils/dy_common.h"
#include "dy_utils/dy_ds.h"
#include "dy_utils/kv_array.h"
#include "dy_utils/dy_ipc.h"

#include "aiot_state_api.h"
#include "aiot_sysdep_api.h"
#include "aiot_mqtt_api.h"
#include "aiot_dm_api.h"
/******************************************/
#define IOTX_PRODUCT_KEY_LEN            (20)
#define IOTX_DEVICE_NAME_LEN            (32)
#define IOTX_DEVICE_SECRET_LEN          (64)
#define IOTX_DEVICE_ID_LEN              (64)
#define IOTX_PRODUCT_SECRET_LEN         (64)
#define IOTX_PARTNER_ID_LEN             (64)
#define IOTX_MODULE_ID_LEN              (64)
#define IOTX_NETWORK_IF_LEN             (160)
#define IOTX_FIRMWARE_VER_LEN           (32)
#define IOTX_URI_MAX_LEN                (135)

typedef struct {

    /* Specify the event type */
    int  event_type;

    /*
     * Specify the detail event information. @msg means different to different event types:
     *
     * 1) IOTX_MQTT_EVENT_UNKNOWN,
     *    IOTX_MQTT_EVENT_DISCONNECT,
     *    IOTX_MQTT_EVENT_RECONNECT :
     *      Its data type is string and the value is detail information.
     *
     * 2) IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS,
     *    IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT,
     *    IOTX_MQTT_EVENT_SUBCRIBE_NACK,
     *    IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS,
     *    IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT,
     *    IOTX_MQTT_EVENT_UNSUBCRIBE_NACK
     *    IOTX_MQTT_EVENT_PUBLISH_SUCCESS,
     *    IOTX_MQTT_EVENT_PUBLISH_TIMEOUT,
     *    IOTX_MQTT_EVENT_PUBLISH_NACK :
     *      Its data type is @uint32_t and the value is MQTT packet identifier.
     *
     * 3) IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
     *      Its data type is @iotx_mqtt_topic_info_pt and see detail at the declare of this type.
     *
     * */
    void *msg;
} iotx_mqtt_event_msg_t, *iotx_mqtt_event_msg_pt;


typedef struct {
	char hostname[IOTX_URI_MAX_LEN];
	uint16_t port;
	char clientid[IOTX_URI_MAX_LEN];
	char username[IOTX_URI_MAX_LEN];
	char password[IOTX_URI_MAX_LEN];
} iotx_sign_mqtt_t;


typedef struct _iotx_dev_meta_info {
	void *pclient;
	char construct;
	
    char product_key[IOTX_PRODUCT_KEY_LEN + 1];
    char device_name[IOTX_DEVICE_NAME_LEN + 1];
    char device_secret[IOTX_DEVICE_SECRET_LEN + 1];
	char alias_sn[32];

	void *dm_handle;
	void *mqtt_handle;
	pthread_t mqtt_process_thread;
	pthread_t mqtt_recv_thread;
	int thread_running;
	
	int communication_fail;
	int post_cnt;
} iotx_dev_meta_info_t;

typedef struct
{
    list_t list;
    iotx_dev_meta_info_t meta_info;
}linkkit_info_list_t;

typedef struct
{
    list_t list;
	int type;
    ipc_msg_t mqtt;
}data_list_t;

typedef struct _alink_var_t
{
    list_t node_list;
	
	int data_flag;
	int data_cnt;
	list_t data_list;
	int link_node_cnt;

	int mqtt_list_cnt;
	list_t send_mqtt_list;
	char response_event[128];
	
	ipc_session_t* session; //内部broker的MQTT client，用于进程间通信
    nodes_cfg_table_t *nodes_cfg_table; //节点信息
    template_table_t *template_table; //模板信息
    char sn_str[SN_MAX_LEN];
    char proc_name[PROGRAM_NAME_LEN];
    kv_array_t identifier_backup;
}alink_var_t;

typedef struct {
    char *dy_sn;
    char *alink_sn;
}alink_iot_sn_map_t;


#endif /* __LINKKIT_GATEWAY_COMMON_H */
