#ifndef __MODBUS_TCP_H__
#define __MODBUS_TCP_H__
#include "tc_system.h"
#include <openssl/ssl.h>
#include <openssl/err.h>

typedef struct MBPA
{
    uint16_t transaction_identifier;
    uint16_t protocol_identifier;
    uint16_t length_field;
    uint8_t unit_id;
}MBPA_t;

typedef struct PDU
{
    uint8_t function_code;
    uint16_t reg_addr;
    uint16_t reg_value;
}PDU_t;

typedef struct report_reg
{
    uint16_t reg_addr;
    uint16_t reg_numbs;
    uint16_t reg_value[0];
}report_reg_t;

typedef struct report_dev
{
    uint16_t dev_addr;
    uint16_t reg_segmts;
    report_reg_t report_regs[0];
}report_dev_t;

typedef struct modbus_TCP
{
    pthread_mutex_t mutex;
    int ssl_enable;
    char* serverip;
    char* port;
    int fd;

    int quit;

    SSL_CTX *ctx;
    SSL *ssl;

    //----------------

    uint8_t  rx_buff[1024];
    int rx_bytes;
    uint8_t  tx_buff[1024];
    int tx_bytes;

    uint8_t  xx_buff[1024];
    int xx_bytes;

    uint16_t trans_ids[2];
    uint16_t seqnumb;

    //----------------
    uint16_t regs_0x0000[0x2B];
    uint16_t regs_0x0100[0x2B];

    //----------------
    uint16_t regs_0x1000[0x26];
    uint16_t regs_0x1100[0xA0];
    uint16_t regs_0x1D00[0xA0];

    uint16_t regs_0x2000[0x17];
    uint16_t regs_0x2100[0x30];
    uint16_t regs_0x2D00[0x30];

    //----------------
    uint16_t regs_0x3000[0x05];
    uint16_t regs_0x3100[0x30];
    uint16_t regs_0x3D00[0x30];

    uint16_t regs_0x4000[0x100];
}modbus_TCP_t;

int modbus_TCP_init(modbus_TCP_t* dev, char* server_ip, char* server_port, int ssl_enable, uint16_t tcu_id);

void* modbus_TCP_loop(void* argv);

void modbus_TCP_exit(modbus_TCP_t* dev);

void showRegs(modbus_TCP_t* dev);

int modbus_TCP_report_registers(modbus_TCP_t* dev, uint16_t regaddr, uint16_t* regvalue, int N);

#endif//__MODBUS_TCP_H__
