#ifndef __CLOUD_MQTT_COMMON_H
#define __CLOUD_MQTT_COMMON_H

/***************HEADER FILES****************/
#include <string.h>
#include <syslog.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>

#include "ipc_session.h"
#include "lnxall_list.h"
#include "dy_utils/dy_common.h"
#include "dy_utils/dy_db.h"
#include "dy_utils/dy_ipc.h"
#include "uart.h"

#define FASTZIGBEE_CFG "/app/config/fastzigbee_cfg_%d.json"
#define SUB_TOPIC "ipc/+/ZIGBEE/device/+/data/Set_Rglt_Raw"

#define DEFAULT_PANID 0x1001
#define DEFAULT_ADDR 0x2001
#define DEFAULT_CH  25

#define DEFAULT_AUTO 0
#define DEFAULT_TYPE 0

// --speed 9600 --stop 1 --parity 2 --bit 8
#define SPEED 57600
#define STOP 1
#define PARITY 0
#define BITS 8

/* 大端序存储 */
typedef struct
{
    char DevName[16];
    char DevPwd[16];
    unsigned char DevMode;
    unsigned char Chan;
    unsigned short PanID;
    unsigned short MyAddr;
    unsigned char MyIEEE[8];
    unsigned short DstAddr;
    unsigned char DstIEEE[8];
    unsigned char Reserve;
    unsigned char PowerLevel;
    unsigned char RetryNum;
    unsigned char TranTimeout;
    unsigned char Serial_Rate;
    unsigned char Serial_DataB;
    unsigned char Serial_StopB;
    unsigned char Serail_ParityB;
    unsigned char Reserve_2;
} __attribute__((packed)) dev_info_t;

typedef struct
{
    node_cfg_t *node;
    int addr;
} node_t;

typedef struct
{
    int node_cnt;
    node_t *nodes;
} node_table_t;

typedef struct fastzigbee_var_s
{
    int fd;
    int slot; //插槽
    dev_info_t *dev_info;

    nodes_cfg_table_t *nodes_cfg_table; //节点信息
    ipc_session_t *session;            //内部broker的MQTT client，用于进程间通信
    node_table_t nodes_table;
    int poll_timer;
    char sn_str[SN_MAX_LEN];
} fastzigbee_var_t;

extern void *recv_loop(void *param);
extern int mqtt_handle_recv_msg(void *obj, ipc_msg_t *mqtt_msg);
extern int modem_send_data(int fd, char *buf, int len, char *rcv_buf, int max_len);

#endif /* __CLOUD_MQTT_COMMON_H */
