#ifndef __MODBUSTCP_TC_H__
#define __MODBUSTCP_TC_H__
#include "msystem.h"

typedef struct modbusTCP_frame
{
    uint16_t transID;
    uint16_t protoID;
    uint16_t datalen;
    uint8_t slaveID;
    uint8_t command;
    // uint8_t data[0];
}modbusTCP_frame_t;

#define modbusTCP_CMD_0x80      0x80
#define modbusTCP_CMD_0x81      0x81
#define modbusTCP_CMD_0x82      0x82
#define modbusTCP_CMD_0x83      0x83

#define modbusTCP_CMD_0x90      0x90
#define modbusTCP_CMD_0x91      0x91
#define modbusTCP_CMD_0x92      0x92
#define modbusTCP_CMD_0x93      0x93
#define modbusTCP_CMD_0x94      0x94
#define modbusTCP_CMD_0x95      0x95
#define modbusTCP_CMD_0x96      0x96

#define modbusTCP_CMD_0xA0      0xA0
#define modbusTCP_CMD_0xA1      0xA1
#define modbusTCP_CMD_0xA2      0xA2
#define modbusTCP_CMD_0xA3      0xA3

#define modbusTCP_CMD_0xB0      0xB0
#define modbusTCP_CMD_0xB1      0xB1
#define modbusTCP_CMD_0xB2      0xB2
#define modbusTCP_CMD_0xB3      0xB3


int encode_U8(uint8_t *stream, int idx, uint8_t units, uint8_t* values);
int decode_U8(uint8_t *stream, int idx, uint8_t units, uint8_t* values);

int encode_U16(uint8_t *stream, int idx, uint8_t units, uint16_t* values);
int decode_U16(uint8_t *stream, int idx, uint8_t units, uint16_t* values);

int encode_U32(uint8_t *stream, int idx, uint8_t units, uint32_t* values);
int decode_U32(uint8_t *stream, int idx, uint8_t units, uint32_t* values);

int encode_U64(uint8_t *stream, int idx, uint8_t units, uint64_t* values);
int decode_U64(uint8_t *stream, int idx, uint8_t units, uint64_t* values);

int encode_HEADER(uint8_t* stream, uint16_t transID, uint8_t slaveID, uint8_t command, uint8_t* values, int length);
int decode_HEADER(uint8_t* stream, uint16_t *transID, uint16_t *protoID, uint16_t *datalen, uint8_t *slaveID, uint8_t *command);
int decode_FRAME(uint8_t* stream, modbusTCP_frame_t* frame);

#endif//__MODBUSTCP_TC_H__
