#ifndef _MY_COMM_H_
#define _MY_COMM_H_


#include <stdint.h>
#include <string>



enum class CommType 
{
    unknown,
    tcp_server,
    tcp_client,
    udp_server,
    udp_client,
    uart,
};

enum class CommDebugShow
{
    off,
    on,
};

enum class CommState
{
    disconnected,
    connected,
};

enum class CommFdType
{
    unknown = 0,
    file,
    socket,
    serial,
    pipe
};

typedef struct 
{   
    std::string local_ip;
    uint16_t local_port;
    std::string remote_ip;
    uint16_t remote_port;
}stru_tcp_para;

typedef struct 
{
    std::string local_ip;
    uint16_t local_port;
    std::string remote_ip;
    uint16_t remote_port;
}stru_udp_para;


typedef struct 
{
    std::string device;
    uint32_t baudrate;
    uint8_t data_bits;
    uint8_t stop_bits;
    uint8_t parity;
}stru_uart_para;

typedef struct 
{
    CommFdType type;
    union 
    {
        struct 
        {
            char local_ip[16];
            int local_port;
            char remote_ip[16];
            int remote_port;
            int protocol;  // SOCK_STREAM or SOCK_DGRAM
        } socket_info;
        
        struct 
        {
            char device[256];
            int baud_rate;
            int data_bits;
            int stop_bits;
            char parity[8];
        } serial_info;
        
        struct 
        {
            char filename[256];
            off_t size;
        } file_info;
    };
}stru_comm_fd_info;


typedef void (*comm_state_cb)(int id, int socket_fd, CommState state);
typedef void (*comm_recv_cb)(int id, int socket_fd, const uint8_t*data, uint16_t len);



int comm_create(CommType type, CommDebugShow debug_show, void *p_para);
int comm_connect(int id);
int comm_disconnect(int id);

int comm_send(int id, int fd,  const uint8_t *data, uint16_t len);
int comm_recv_register(int id, comm_recv_cb cb);
int comm_state_register(int id, comm_state_cb cb);


#endif