#ifndef _MODULE_WRAPPER_H_
#define _MODULE_WRAPPER_H_
#include <stdint.h>
#include <stdio.h>
#include "sys.h"

#define SEND_SIZE 1024   //发送缓存大小
#define CHANNEL_COUNT 2    //网络链接通道数量
#define MIN_CHANNEL_NUM 1    //最小通道号

/** 连接通道信息*/
typedef struct module_channel_st {
	  uint8_t proto;
    uint8_t connect_status;
    valid_data_t valid_data;
} module_channel_t;

extern uint8 tcp_connect_status;

extern uint8_t module_start_status;

extern uint8 module_init_status; 

extern module_channel_t module_channel[CHANNEL_COUNT];

typedef void (*at_event_callback_t)(void);

typedef struct at_event_st {
    const char         *event_header;
    at_event_callback_t event_callback;
} at_event_t;


enum tcp_status_en
{
	TCP_CLOSED, //tcp关闭
	TCP_OPEN, //tcp打开
	TCP_CONNECTING //tcp正在连接
};


enum module_proto_en
{
	UDP, //UDP协议
	TCP, //TCP协议
};

enum module_status_en
{
	MODULE_FAIL, //无效
	MODULE_OK, //有效
};

enum send_rai_en
{
	KEPP_RRC, //不释放RRC
	NO_RRC, //立即释放RRC
	ONE_RRC //返回释放RRC
};


typedef struct at_module_st {
		
		void (*start)(void);
	
    int (*init)(void);

    void (*connect)(const char *ip, const char *port);
	
		void (*udpopen)(const char *ip, const char *port);
	
	  void (*reconnect)(const char *ip, const char *port);
	
		void (*udpreopen)(const char *ip, const char *port);
	  
	  void (*channel_open)(const char *ip, const char *port,uint8_t proto, uint8_t channel);
		
		void (*channel_reopen)(const char *ip, const char *port,uint8_t proto, uint8_t channel);
	
		int (*tcpstate)(void);
	
	  int (*channel_state)(uint8_t channel);

		int (*send)(const void *buf, int len);
	
		int (*send_rai)(const void *data, int len,int rai);
		
		int (*send_ch)(const void *data, int len,uint8_t channel,int rai);
	
		int (*recv_timeout)(void *buf, int len, int timeout);
	
    int (*close)(void);
		
		int (*channel_close)(uint8_t channel);
	
	  void (*handle_event)(void);
		
		void (*cmd_back)(char *cmd, char *data, int size);
		
		void (*cmd)(char *cmd);
	
		int (*low_power)(void);
} at_module_t;

void channel_data_clear(uint8_t channel);

int at_module_register(at_module_t *module);

int at_module_register_default(void);

void at_module_start(void);

int at_module_init(void);

void at_module_connect(const char *ip, const char *port);

void at_module_udpopen(const char *ip, const char *port);

void at_module_reconnect(const char *ip, const char *port);

void at_module_udpreopen(const char *ip, const char *port);

void at_module_channel_open(const char *ip, const char *port,uint8_t proto, uint8_t channel);

void at_module_channel_reopen(const char *ip, const char *port,uint8_t proto, uint8_t channel);

int at_module_close(void);

int at_module_channel_close(uint8_t channel);

int at_module_tcpstate(void);

int at_module_channel_state(uint8_t channel);

int at_module_send(const void *buf, size_t len);

int at_module_send_rai(const void *buf, size_t len, int rai);

int at_module_send_ch(const void *buf, size_t len,uint8_t channel, int rai);

int at_module_recv_timeout(void *buf, int len, int timeout);

void at_module_handle_event(void);

void at_module_cmd_back(char *cmd, char *data, int size);

void at_module_cmd(char *cmd);

void at_module_low_power(void);

#endif
