#include "includes.h"
#include "net_ctrl.h"
#include "net_proc.h"
#include "at_module.h"
#include "me3616.h"
#include "tax_ctrl.h"
#include "gateway_collect.h"

sverMsgHeader g_msg;
extern uint16_t _crc16_get(uint8_t *_buff,uint32_t _len);

extern ME3616  air;
uint8_t net_send_buf[1024],net_send_len=0;
uint8_t net_seng_state = 0;

uint8_t net_rcv_data[512];
uint16_t net_rcv_len = 0;
uint8_t net_rcv_state = 0;

//collect_para_t collect_para[4];
//tax_para_t tax_para[4][4];

uint8_t AIR_MQTT_PUB(char * topic, char *data, uint32_t len);

uint16_t crc16_get(uint8_t *data, uint8_t size)
{
	uint16_t crc=0;
	int i;
	for(i = 0; i < size; i++)
	{
		crc += data[i];
	}
	return crc;
}

int tax_net_send(uint8_t *tx_data, uint16_t len, uint8_t typeFirst,uint16_t typeSecd)
{

    char *msg = NULL;
    OS_ERR      err;
    static uint32_t seq_num = 0;
    uint16_t crc = 0;
   // sverMsgHeader p_msg;// = (sverMsgHeader *)net_send_buf;
   // memset(net_send_buf, 0, sizeof(net_send_buf));

   if(sys_net.net_hdl==NULL) return 0;

    memset(g_msg.info,0,sizeof(g_msg.info));

    g_msg.frame_header = 0xfefe;
    g_msg.proto_ver = 0x01;
    g_msg.seq_no = seq_num++;

    g_msg.msgtypeFirst = typeFirst;
    g_msg.msgtypeSecd = typeSecd;
    g_msg.len = len + 2;
    //p_msg->fcs = 0;

    memcpy(g_msg.info, tx_data, len);
    crc = _crc16_get((uint8_t *)&g_msg,len+12);
    g_msg.info[len] = crc&0xff;
    g_msg.info[len+1] = (crc>>8)&0xff;


    net_send_len = len+FRAME_HEADER_LEN+2;

    msg = (char *)net_queue_mem_calloc_must();
    if(msg) {
        memcpy(msg, (char*)&g_msg, len + FRAME_HEADER_LEN+2);
        net_queue_insert((char *)msg, len + FRAME_HEADER_LEN+2);
    }
    else
    {
        net_queue_mem_free(msg);
    }
    return 0;
}

//void serino_init(void)
//{
//	uint8_t monitor_serino[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//	uint8_t coder_serino[20] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x11,0x22,0x33,0x44,0x55};
//	uint8_t dispaly_serino[16] = {0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,0x9A,0xAB,0xBC,0xCD,0xDE,0xEF,0xF0,0xFF};

//	collect_para[0].device_id = 0x01;;
//	collect_para[0].device_status = 0x00;
//	collect_para[0].tax_control_count = 0x02;
//	
//	collect_para[1].device_id = 0x02;;
//	collect_para[1].device_status = 0x00;
//	collect_para[1].tax_control_count = 0x03;
//	
//	memcpy(tax_para[0][0].tax_serino,monitor_serino,16);
//	memcpy(tax_para[0][0].coder_serino,coder_serino,20);
//	memcpy(tax_para[0][0].dispaly_serino,dispaly_serino,16);
//	tax_para[0][0].gun_count = 0x02;
//}

//int tax_data(uint8_t *data, uint8_t *len)
//{
//	uint8_t monitor_serino[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//	uint8_t coder_serino[20] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x11,0x22,0x33,0x44,0x55};
//	uint8_t dispaly_serino[16] = {0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,0x9A,0xAB,0xBC,0xCD,0xDE,0xEF,0xF0,0xFF};
//	
//	uint8_t tx_data[128];
//	
//	tax_comm_info_t *p_msg = (tax_comm_info_t *)tx_data;
//	
//	memset(tx_data,0,sizeof(tx_data));
//	p_msg->device_type = 0x01;
//	p_msg->device_id = 0x02;
//	p_msg->device_status = 0x00;
//	p_msg->tax_control_count = 0x01;
//	
//	memcpy(p_msg->tax_control_info.monitor_serino,monitor_serino,16);
//	memcpy(p_msg->tax_control_info.coder_serino,coder_serino,20);
//	memcpy(p_msg->tax_control_info.dispaly_serino,dispaly_serino,16);
//	p_msg->tax_control_info.gun_count = 0x01;
//	
//	p_msg->tax_control_info.guns_info.gun_number = 0x01;
//	p_msg->tax_control_info.guns_info.gun_status = 0x00;
//	p_msg->tax_control_info.guns_info.total_price = 0x4358;
//	p_msg->tax_control_info.guns_info.total_oil_volume = 0x2567;
//	p_msg->tax_control_info.guns_info.last_unit_price = 0x96;
//	p_msg->tax_control_info.guns_info.last_oil_volume = 0x84;
//	p_msg->tax_control_info.guns_info.last_price = 0x200;
//	
//	*len = 85;
//	memcpy(data,tx_data,*len);
//}

//int tax_net_data_analyze(void *puser,uint8_t collect, uint8_t tax, uint8_t *outdata, uint8_t *len)
//{
////	uint8_t tx_data[128];
//	int ret = 0;
//	gun_info_t * p_gun = (gun_info_t *)puser;
//	
//	tax_comm_info_t *p_msg = (tax_comm_info_t *)outdata;

//	p_msg->device_type = 1;
//	p_msg->device_id = collect_para[collect].device_id;
//	p_msg->device_status = collect_para[collect].device_status;
//	p_msg->tax_control_count = collect_para[collect].tax_control_count;
//	
////	printf("device_id : %d\n",p_msg->device_id);
////	printf("device_status : %d\n",p_msg->device_status);
////	printf("tax_control_count : %d\n",p_msg->tax_control_count);
//	
//	memcpy(p_msg->tax_control_info.monitor_serino,tax_para[collect][tax].tax_serino,16);
//	memcpy(p_msg->tax_control_info.coder_serino,tax_para[collect][tax].coder_serino,20);
//	memcpy(p_msg->tax_control_info.dispaly_serino,tax_para[collect][tax].dispaly_serino,16);
//	p_msg->tax_control_info.gun_count = tax_para[collect][tax].gun_count;
//	
//	p_msg->tax_control_info.guns_info.gun_number = p_gun->id;
//	p_msg->tax_control_info.guns_info.gun_status = p_gun->status;
//	p_msg->tax_control_info.guns_info.gun_type = p_gun->gun_type;
//	p_msg->tax_control_info.guns_info.gun_msgid = p_gun->gun_msgid;
//	if(p_gun->gun_type == SINGLE_DATA)
//	{
//		p_msg->tax_control_info.guns_info.last_oil_volume = p_gun->last_oil_volume;
//		p_msg->tax_control_info.guns_info.last_price = p_gun->last_price;
//		p_msg->tax_control_info.guns_info.last_unit_price = p_gun->last_unit_price;
//	}
//	else if(p_gun->gun_type == CUMULATIVE_DATA)
//	{
//		p_msg->tax_control_info.guns_info.total_oil_volume = p_gun->total_oil_volume;
//		p_msg->tax_control_info.guns_info.total_price = p_gun->total_price;
//	}
//	
//	*len = COLLECT_COM;
//	return ret;
//}

//void gw_net_comm(uint8_t *data, uint8_t len)
//{
//	uint8_t send_buf[128],send_size;
//	uint8_t tmp_buf[128];
//	
//	
////	collect_rcv_analyze(data,len,tmp_buf);
////	tax_net_data_analyze();
//	tax_net_send(send_buf,send_size,1);
//}


//void gw_net_comm_test(void)
//{
//	uint8_t send_buf[128],send_size;
//	
//	tax_data(send_buf, &send_size);
//	
//	tax_net_send(send_buf,send_size,1);
//}

int air_mqtt_pub(char *topic, char *data, uint8_t len)
{
    int slen,slen1,send_len=0;
    uint8_t sendbuf[512];

    if(!air.State.MQTT_State) {
        ME3616_INFO("Cannot Connect MQTT");
        return 0;
    }

    slen=sprintf((char *)sendbuf,"AT+MPUB=\"%s\",0,0,\"",topic);
    send_len = slen;
    memcpy(sendbuf+send_len,data,len);
    send_len += len;
    slen1=sprintf((char *)sendbuf+send_len,"\"\r\n");
    send_len += slen1;
    uart_msg_send(UART3_ID, (char *)sendbuf, send_len);
//	uart_msg_send(UART1_ID, (char *)sendbuf, send_len);

    if(Module_Read_A_CRC("OK", 300)) {
            //ME3616_INFO("Pub Successful \r\n[%s]<-[%s]",topic,data);
        return 1;
    } else {
        ME3616_INFO("Pub ERROR");
        return 0;
    }
}


void gw_net_send(char *data, uint32_t len)
{
    int i;
   // char send_buf[320];
    int send_len=0;
    if(air.State.MQTT_State)
    {
        memset(net_send_buf,0,sizeof(net_send_buf));
        for(i = 0;i < len;i++) {
            send_len += snprintf((char *)net_send_buf + send_len, sizeof(net_send_buf), "%02x", data[i]);
        }
        g_data4G.bconnect4G = AIR_MQTT_PUB((char*)g_upLinkTopic,(char*)net_send_buf,send_len);
//				AIR_MQTT_PUB((char*)g_upLinkTopic,data,len);
			    
	//		    AIR_MQTT_PUB((char*)g_upLinkTopic,data,len);
    }
}


////////////////////////////////
void net_msg_recv1(void)
{
	int len = 0;
	char *str,*str1,*str2;
	char *msg = NULL;
	OS_ERR      err;
	int slen,data_len;
	if(!air.State.MQTT_State) {
		return;
	}
	
        return;

	memset(sys_net.str, 0, sizeof(sys_net.str));	
	len = Module_Blocking_Read(sys_net.str, 1);
	if(len) {
            len = Module_Blocking_Read(sys_net.str+len, 100);
            str = strstr(sys_net.str, "byte,");
            str1 = strstr(sys_net.str,"\r\n");
            if(strstr(sys_net.str,"+MQTTSTATU:")){
                printf("\r\n aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n");
            }
            if(str && str1) {

                    slen = net_data_copy(str+5,(uint8_t *)sys_net.str);
                    net_rcv_len = net_rcvdata_ch((char *)sys_net.str,net_rcv_data,slen);

                    data_dump("NET RECB", net_rcv_data, net_rcv_len);
                    msg = (char *)net_queue_mem_calloc_must();
                    if(msg) {
                            memcpy(msg, net_rcv_data, net_rcv_len);
                            net_queue_insert((char *)msg, net_rcv_len); //将数据放到消息队列中
                    }
                    else
                    {
                            net_queue_mem_free(msg);
                    }

            }
    }
}


unsigned char HexToChar(unsigned char bChar)
{
	if((bChar>=0x30)&&(bChar<=0x39))
	{
		bChar -= 0x30;
	}
	else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
	{
		bChar -= 0x37;
	}
	else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
	{
		bChar -= 0x57;
	}
	else 
	{
		bChar = 0xff;
	}
	return bChar;
}

int net_rcvdata_ch(char *data, uint8_t *out_data, int len)
{
	int i,slen=0;
	uint8_t temp1,temp2;
	for(i = 0; i < len; i+=2)
	{
		temp1 = HexToChar(data[i]);
		temp2 = HexToChar(data[i+1]);
		out_data[slen] = (temp1<<4) | temp2;
		slen++;
	}
	return slen;
}


int net_data_copy(char *data, uint8_t *out_data)
{
	int len = 0;
	int i=0;
	while(1)
	{
		if(data[len] == '\r'||data[len] == '\n')
			break;
		
		out_data[len] = data[len];
		len++;
		
//		if(len >= 512)
//			break;
	}
	return len;
}


