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

gb_device_info_t	dev_mgr;

void device_info_dump(inner_msg_format_t *p_msg)
{
	gb_device_info_t *p_dev = (gb_device_info_t *)p_msg->info;
	
	printf("frame_header: 0x%04x\r\n", p_msg->frame_header);
	printf("fcs: 0x%04x\r\n", p_msg->fcs);
	printf("seq_no: 0x%08x\r\n", p_msg->seq_no);
	printf("proto_ver: 0x%02x\r\n", p_msg->proto_ver);
	printf("type: 0x%02x\r\n", p_msg->type);
	printf("len: 0x%04x\r\n", p_msg->len);
	printf("vendor: %s\r\n", p_dev->vendor);
	printf("hw_ver: 0x%08x\r\n", p_dev->hw_ver);
	printf("manufacture_date: %d\r\n", p_dev->manufacture_date);
	printf("bootrom_ver: 0x%08x\r\n", p_dev->bootrom_ver);
	printf("bootrom_date: %d\r\n", p_dev->bootrom_date);
	printf("sw_ver: 0x%08x\r\n", p_dev->sw_ver);
	printf("build_date: %d\r\n", p_dev->build_date);
	printf("device_type: 0x%08x\r\n", p_dev->device_type);
	printf("device_id: 0x%08x\r\n", p_dev->device_id);
	printf("imei: %s\r\n", p_dev->imei);
	printf("iccid: %s\r\n", p_dev->iccid);
	printf("collect_time: %d\r\n", p_dev->collect_time);	
}

//void device_msg_timer(void *p_tmr, void *p_arg)
//{
//	inner_msg_format_t *p_msg = NULL;

//	dev_mgr.collect_time = RTC_GetCounter();
//	p_msg = (inner_msg_format_t *)net_queue_mem_calloc();
//	if(p_msg) {
//		memcpy(p_msg->info, &dev_mgr, sizeof(gb_device_info_t));
//		net_msg_frame_fill(p_msg, net_msg_type_lot, sizeof(gb_device_info_t));
//		//device_info_dump(p_msg);
//		net_queue_insert((char *)p_msg, sizeof(inner_msg_format_t));
//		//data_dump(MQTT_UPLINK_LOT_TOPIC, (uint8_t *)p_msg, sizeof(inner_msg_format_t));
//	}
//}

int device_type_and_id_cmp(u16 device_type, u32 device_id)
{
	int ret = 0;

        if(device_type != 0x0301) {
            return ret;
        }
        else if(device_id == 0xFFFFFFFF) {
		ret = 1;
	} else {					
		if((device_type == dev_mgr.device_type)&&
				(device_id == dev_mgr.device_id)) {
			ret = 1;
		}
	}

	return ret;
}

void device_type_and_id_get(u32 *device_type, u32 *device_id)
{
	if(device_type) {
		*device_type = dev_mgr.device_type;
	}
	if(device_id) {
		*device_id = dev_mgr.device_id;
	}
}

uint32_t device_type_get(void)
{
	return dev_mgr.device_type;
}

OS_TMR dev_timer;
int device_init(u32 dev_type, u32 dev_id)
{
	OS_ERR err;
	gb_device_info_t *p_dev_mgr = &dev_mgr;
	uint32_t temp;

	memset(p_dev_mgr, 0, sizeof(gb_device_info_t));
	snprintf((char *)p_dev_mgr->vendor, sizeof(p_dev_mgr->vendor), "goldenbeans");
	p_dev_mgr->sw_ver = SOFT_VERSION;
	p_dev_mgr->build_date = 0;

	/* read from e2prom. */
	//AT24CXX_Read(0, (uint8_t *)&p_dev_mgr->device_type, 8);
	p_dev_mgr->device_type = dev_type;
	p_dev_mgr->device_id = dev_id;
	p_dev_mgr->hw_ver = HARD_VERSION;
	p_dev_mgr->manufacture_date = 0;
	p_dev_mgr->bootrom_ver = BOOT_VERSION;
	p_dev_mgr->bootrom_date = 0;

	NET_IMEI_ICCID_INFO_GET((char *)p_dev_mgr->imei, (char *)p_dev_mgr->iccid);

//	OSTmrCreate(&dev_timer, "device_tmr", DEVICE_MSG_PERIOD + 5, DEVICE_MSG_PERIOD, OS_OPT_TMR_PERIODIC, device_msg_timer, NULL, &err);
//	OSTmrStart(&dev_timer, &err);	

	return 0;
}
