#include "includes.h"
#include "device.h"
#include "gateway.h"
#include "net_proc.h"

typedef struct _gb_gateway_info {
	gb_gateway_bind_info_t bind;
	u16 gw_echo_fail_count;
	u16 node_echo_fail_count[GB_SUPPORT_MAX_NODE];
} gb_gateway_info_t;

gb_gateway_info_t gw_mgr;

int gateway_gw_id_flush(u32 gw_device_id)
{
	int ret = 0;
	gw_mgr.bind.gw_device_id = gw_device_id;
	gw_mgr.gw_echo_fail_count = 0;
	
	return ret;
}
	
int gateway_id_cmp(u32 gw_device_id)
{
	int ret = 0;
	
	if(gw_mgr.bind.gw_device_id == gw_device_id) {
		ret = 1;
	}

	return ret;
}

int gateway_gw_id_get(u32 *gw_device_id)
{
	int ret = 0;

	if(gw_device_id) {
		*gw_device_id = gw_mgr.bind.gw_device_id;
	}
	
	if(gw_mgr.bind.gw_device_id) {
		ret = 1;
	}
	
	return ret;
}

int gateway_id_and_chn_get(u32  des_device_type, u32 des_device_id, 
				u32 *gw_device_id, uint8_t *chn)
{
	int ret = 0, i = 0;
	
	if(gw_device_id) {
		*gw_device_id = gw_mgr.bind.gw_device_id;
	}

	if(chn) {
		for(i = 0;i < GB_SUPPORT_MAX_NODE;i++) {
			gb_node_info_t *p_node = &gw_mgr.bind.node[i];
			if((p_node->device_type == des_device_type)&&
				(p_node->device_id == des_device_id)) {
				*chn = i;
				ret = 1;//find
				break;
			}
		}
	}

	return ret;
}

int gateway_node_type_and_id_flush(u32  des_device_type, u32 des_device_id)
{
	int i = 0, choice = 0xff;
	gb_node_info_t *p_node = NULL;
	
	for(i = 0;i < GB_SUPPORT_MAX_NODE;i++) {
		p_node = &gw_mgr.bind.node[i];
		if((p_node->device_type == des_device_type)&&
			(p_node->device_id == des_device_id)) {
			gw_mgr.node_echo_fail_count[i] = 0;
			break;
		} else if((choice == 0xff) &&(p_node->device_type == 0)) {
			choice = i;
		}
	}

	if((i == GB_SUPPORT_MAX_NODE)&&(choice != 0xff)) {/* bind */
		p_node = &gw_mgr.bind.node[choice];
		p_node->device_type = des_device_type;
		p_node->device_id = des_device_id;
		gw_mgr.node_echo_fail_count[choice] = 0;
	}

	return 0;
}

//void gateway_msg_timer(void *p_tmr, void *p_arg)
//{
//	inner_msg_format_t *p_msg = NULL;
//	u8 type = 0, len = 0, i;

//	p_msg = (inner_msg_format_t *)net_queue_mem_calloc();
//	if(p_msg == NULL) {
//		return ;
//	}
//	
//	if((device_type_get() == DEV_TYPE_COLLECT_STATION)||
//		(device_type_get() == DEV_TYPE_OILTANK_STATION)) {/* collect */
//		gb_node_bind_info_t *p_node_bind = (gb_node_bind_info_t *)p_msg->info;
//		if(gw_mgr.bind.gw_device_id == 0) {	
//			p_node_bind->gw_device_id = 0;
//			type = net_msg_type_bind_req;
//		} else {
//			p_node_bind->gw_device_id = gw_mgr.bind.gw_device_id;
//			type = net_msg_type_node_echo;
//		}
//		device_type_and_id_get(&p_node_bind->device_type, &p_node_bind->device_id);
//		len = sizeof(gb_node_bind_info_t);
//		gw_mgr.gw_echo_fail_count++;
//		if(gw_mgr.gw_echo_fail_count > GATEWAY_ECHO_MAX_COUNT) {/* aging. */
//			gw_mgr.bind.gw_device_id = 0;			
//		}
//	} else if(device_type_get() == DEV_TYPE_GATEWAY){/* gateway */
//		type = net_msg_type_gw_echo;
//		memcpy(p_msg->info, &gw_mgr.bind, sizeof(gb_gateway_bind_info_t));
//		len = sizeof(gb_gateway_bind_info_t);
//		for(i = 0;i < GB_SUPPORT_MAX_NODE;i++) {
//			gw_mgr.node_echo_fail_count[i]++;
//			if(gw_mgr.node_echo_fail_count[i] > GATEWAY_ECHO_MAX_COUNT) {/* aging. */
//				gw_mgr.bind.node[i].device_type = 0;
//				gw_mgr.bind.node[i].device_id = 0;
//			}
//		}
//	}
//	
//	net_msg_frame_fill(p_msg, type, len);
//	net_queue_insert((char *)p_msg, sizeof(inner_msg_format_t));
//}

OS_TMR gateway_timer;
int gateway_init(void)
{
//	OS_ERR err;

//	if(device_type_get() == DEV_TYPE_GATEWAY) {
//	 	device_type_and_id_get(NULL, &gw_mgr.bind.gw_device_id);
//	}
//	OSTmrCreate(&gateway_timer, "gateway_tmr", 
//		GW_MSG_PERIOD, GW_MSG_PERIOD, OS_OPT_TMR_PERIODIC, gateway_msg_timer, NULL, &err);
//	OSTmrStart(&gateway_timer, &err);	

	return 0;
}
