/**
* author wulianwei
* title 通信模块驱动封装
*/
#include "module_wrapper.h"
#include "string.h"



static at_module_t *g_at_module = NULL;

uint8 tcp_connect_status = TCP_CLOSED; //tcp连接状态  0:断开,1:连接,2 连接中
uint8_t module_start_status = MODULE_FAIL; //模块启动
uint8  module_init_status = MODULE_FAIL; // 模块初始化 

module_channel_t module_channel[CHANNEL_COUNT]={0};

void channel_data_clear(uint8_t channel)
{
	module_channel[channel].valid_data.len = 0;
	memset(module_channel[channel].valid_data.buf,0,sizeof(module_channel[channel].valid_data.buf));
}


int at_module_register(at_module_t *module)
{
    if (!g_at_module) {
        g_at_module = module;
        return 0;
    }

    return -1;
}

int at_module_register_default()
{
    g_at_module = NULL;

    return 0;
}

void at_module_start(void)
{
    if (g_at_module && g_at_module->start) {
        g_at_module->start();
    }
}

int at_module_init(void)
{
    if (g_at_module && g_at_module->init) {
        return g_at_module->init();
    }
		return -1;
}

void at_module_connect(const char *ip, const char *port)
{
    if (g_at_module && g_at_module->connect) {
        g_at_module->connect(ip, port);
    }
}

void at_module_udpopen(const char *ip, const char *port)
{
    if (g_at_module && g_at_module->udpopen) {
        g_at_module->udpopen(ip, port);
    }
}


void at_module_reconnect(const char *ip, const char *port)
{
    if (g_at_module && g_at_module->reconnect) {
        g_at_module->reconnect(ip, port);
    }
}

void at_module_udpreopen(const char *ip, const char *port)
{
    if (g_at_module && g_at_module->udpreopen) {
        g_at_module->udpreopen(ip, port);
    }
}

void at_module_channel_open(const char *ip, const char *port,uint8_t proto, uint8_t channel)
{
    if (g_at_module && g_at_module->channel_open) {
        g_at_module->channel_open(ip, port,proto,channel);
    }
}

void at_module_channel_reopen(const char *ip, const char *port,uint8_t proto, uint8_t channel)
{
    if (g_at_module && g_at_module->channel_reopen) {
        g_at_module->channel_reopen(ip, port,proto,channel);
    }
}

int at_module_close(void)
{
    if (g_at_module && g_at_module->close) {
        g_at_module->close();
    }
}

int at_module_channel_close(uint8_t channel)
{
    if (g_at_module && g_at_module->channel_close) {
        g_at_module->channel_close(channel);
    }
}

int at_module_tcpstate(void)
{
    if (g_at_module && g_at_module->tcpstate) {
        return g_at_module->tcpstate();
    }
    return -1;
}

int at_module_channel_state(uint8_t channel)
{
    if (g_at_module && g_at_module->channel_state) {
        return g_at_module->channel_state(channel);
    }
    return -1;
}

int at_module_send(const void *buf, size_t len)
{
    if (g_at_module && g_at_module->send) {
        return g_at_module->send(buf, len);
    }
    return -1;
}

int at_module_send_rai(const void *buf, size_t len, int rai)
{
    if (g_at_module && g_at_module->send_rai) {
        return g_at_module->send_rai(buf, len, rai);
    }
    return -1;
}

int at_module_send_ch(const void *buf, size_t len,uint8_t channel, int rai)
{
    if (g_at_module && g_at_module->send_ch) {
        return g_at_module->send_ch(buf, len,channel, rai);
    }
    return -1;
}

int at_module_recv_timeout(void *buf, int len, int timeout)
{
    if (g_at_module && g_at_module->recv_timeout) {
        return g_at_module->recv_timeout(buf, len ,timeout);
    }
    return -1;
}

void at_module_handle_event(void)
{
	
    if (g_at_module && g_at_module->handle_event) {
        g_at_module->handle_event();
    }
}

void at_module_cmd_back(char *cmd, char *data, int size)
{
	if (g_at_module && g_at_module->cmd_back) {
        g_at_module->cmd_back(cmd,data,size);
    }
}

void at_module_cmd(char *cmd)
{
	if (g_at_module && g_at_module->cmd) {
        g_at_module->cmd(cmd);
    }
}

void at_module_low_power(void)
{
    if (g_at_module && g_at_module->low_power) {
        g_at_module->low_power();
    }
}

