Exemplo n.º 1
0
static void s2e_sockestablished_process(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;

	if(reconn_flag)
		reconn_flag = reconn_time = 0;

	switch(net->working_mode) {
		case TCP_MIXED_MODE:
		case TCP_SERVER_MODE:
			if(auth_flag) {
				auth_process(sock);
				return;
			}
		case TCP_CLIENT_MODE:
			if((inactive_flag == 0) && net->inactivity)
				inactive_flag = 1;
			else if(inactive_flag == 2) {
				inactive_flag = 0;
				disconnect(sock);
			}
			ether_to_uart(sock);
			uart_to_ether(sock);
			break;

		case UDP_MODE:
			close(sock);
			break;

		default:
			break;
	}
}
Exemplo n.º 2
0
static void trigger_state2_process()
{
	struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options);
	int len = 0;

	if(trigger_flag == 2) {
		trigger_state = TRIG_STATE_NONE;
#ifdef __TRIG_DEBUG__
		printf("[%s] TRIG_STATE_NONE #1\r\n", __func__);
#endif
		trigger_flag = uart_size_prev = 0;
		return;
	}

	if((len = RingBuffer_SerachPattern(&rxring, pattern_offset - 2, option->serial_trigger, 3))) {
		if((pattern_cnt + 1) == len) {
			trigger_state = TRIG_STATE_3;
#ifdef __TRIG_DEBUG__
			printf("TRIG_STATE_3\r\n");
#endif
			trigger_time = 0;
			pattern_offset = pattern_cnt = len;
			uart_size_prev = RingBuffer_GetCount(&rxring);
			return;
		}
	}

	if(pattern_cnt < RingBuffer_GetCount(&rxring)) {
		trigger_state = TRIG_STATE_NONE;
#ifdef __TRIG_DEBUG__
		printf("[%s] TRIG_STATE_NONE #2\r\n", __func__);
#endif
		trigger_flag = trigger_time = uart_size_prev = 0;
	}
}
Exemplo n.º 3
0
void w5500_dhcp_assign()
{
	S2E_Packet *value = get_S2E_Packet_pointer();
	wiz_NetInfo gWIZNETINFO;

	getIPfromDHCP(gWIZNETINFO.ip);
	getGWfromDHCP(gWIZNETINFO.gw);
	getSNfromDHCP(gWIZNETINFO.sn);
	getDNSfromDHCP(gWIZNETINFO.dns);

	get_S2E_Packet_value(gWIZNETINFO.mac, value->network_info_common.mac, sizeof(gWIZNETINFO.mac[0]) * 6);
	set_S2E_Packet_value(value->network_info_common.local_ip, gWIZNETINFO.ip, sizeof(value->network_info_common.local_ip));
	set_S2E_Packet_value(value->network_info_common.gateway, gWIZNETINFO.gw, sizeof(value->network_info_common.gateway));
	set_S2E_Packet_value(value->network_info_common.subnet, gWIZNETINFO.sn, sizeof(value->network_info_common.subnet));
	set_S2E_Packet_value(value->options.dns_server_ip, gWIZNETINFO.dns, sizeof(value->options.dns_server_ip));
	if(value->options.dhcp_use)
		gWIZNETINFO.dhcp = NETINFO_DHCP;
	else
		gWIZNETINFO.dhcp = NETINFO_STATIC;

	ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);

//	display_Net_Info();
//	printf("DHCP LEASED TIME : %d sec. \r\n", getDHCPLeasetime());
}
Exemplo n.º 4
0
static void trigger_none_process(uint8_t sock_state)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;

	if(trigger_flag == 2) {
		trigger_state = TRIG_STATE_READY;
#ifdef __TRIG_DEBUG__
		printf("TRIG_STATE_READY\r\n");
#endif
		trigger_flag = 0;
		uart_size_prev = RingBuffer_GetCount(&rxring);
		return;
	}

	if(uart_size_prev == RingBuffer_GetCount(&rxring)) {			// UART ?�신 ?�이?��? ?�으�?
		if(trigger_flag == 0)
			trigger_flag = 1;
	} else {
		trigger_flag = trigger_time = 0;
		uart_size_prev = RingBuffer_GetCount(&rxring);
		if((sock_state != SOCK_ESTABLISHED) && (sock_state != SOCK_UDP) && (net->working_mode != TCP_MIXED_MODE)) {
			UART_buffer_flush(&rxring);
			uart_size_prev = 0;
		}
	}
}
static void process_set_info(uint8_t sock, Remote_Info *remote_info, WIZnet_Header *wiznet_header)
{
	uint16_t len;
	uint8_t buffer[sizeof(WIZnet_Header) + sizeof(WIZnet_Set_Info_Reply)];
	uint32_t ptr;
	WIZnet_Set_Info set_info;
	WIZnet_Set_Info_Reply reply;
	S2E_Packet *s2e_packet = get_S2E_Packet_pointer();
	uint8_t ip[4];
	uint16_t port;

	getsockopt(sock, SO_REMAINSIZE, &len);
	if(len != wiznet_header->length)
		return;

	recvfrom(sock, (uint8_t *)&set_info, sizeof(WIZnet_Set_Info), ip, &port);

	if((wiznet_header->valid & 0x80))
		decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&set_info, len);

	if(memcmp(set_info.dst_mac_address, s2e_packet->network_info_common.mac, 6))
		return;

	if(strcmp((char *)set_info.set_pw, (char *)s2e_packet->options.pw_setting))
		return;

	//update config msg
	set_S2E_Packet_value(s2e_packet, &set_info.system_info, sizeof(S2E_Packet));
	s2e_packet->fw_ver[0] = MAJOR_VER;
	s2e_packet->fw_ver[1] = MINOR_VER;
	s2e_packet->fw_ver[2] = MAINTENANCE_VER;

	//save to storage
	save_S2E_Packet_to_storage();

	// reply
	wiznet_header->length = sizeof(WIZnet_Set_Info_Reply);
	wiznet_header->op_code[1] = WIZNET_REPLY;

	memcpy(reply.src_mac_address, s2e_packet->network_info_common.mac, 6);
	memcpy(&reply.system_info, s2e_packet, sizeof(S2E_Packet));

	if((wiznet_header->valid & 0x80))
		encrypt((wiznet_header->valid & 0x7F), (uint8_t *)&reply, sizeof(WIZnet_Set_Info_Reply));

	ptr = 0;
	memcpy(buffer, wiznet_header, sizeof(WIZnet_Header));
	ptr += sizeof(WIZnet_Header);
	memcpy(buffer + ptr, (void *)&reply, sizeof(WIZnet_Set_Info_Reply));
	ptr += sizeof(WIZnet_Set_Info_Reply);

	if(wiznet_header->unicast == 0) {
		memset(remote_info->ip, 255, 4);
	}
	sendto(sock, buffer, ptr, remote_info->ip, remote_info->port);

	delay_cnt(100);
	NVIC_SystemReset();
}
static void process_factory_reset(uint8_t sock, Remote_Info *remote_info, WIZnet_Header *wiznet_header)
{
	uint16_t len;
	uint8_t buffer[sizeof(WIZnet_Header) + sizeof(WIZnet_Factory_Reset_Reply)];
	uint32_t ptr;
	WIZnet_Factory_Reset factory_reset;
	WIZnet_Factory_Reset_Reply reply;
	S2E_Packet *s2e_packet = get_S2E_Packet_pointer();
	uint8_t ip[4];
	uint16_t port;

	getsockopt(sock, SO_REMAINSIZE, &len);
	if(len != wiznet_header->length)
		return;

	recvfrom(sock, (uint8_t *)&factory_reset, sizeof(WIZnet_Factory_Reset), ip, &port);

	if((wiznet_header->valid & 0x80))
		decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&factory_reset, len);

	if(memcmp(factory_reset.dst_mac_address, s2e_packet->network_info_common.mac, 6))
		return;

	if(strcmp((char *)factory_reset.set_pw, (char *)s2e_packet->options.pw_setting))
		return;

	// set factory value
	set_S2E_Packet_to_factory_value();
	save_S2E_Packet_to_storage();

	// reply
	wiznet_header->length = sizeof(WIZnet_Reset_Reply);
	wiznet_header->op_code[1] = WIZNET_REPLY;

	memcpy(reply.src_mac_address, s2e_packet->network_info_common.mac, 6);

	if((wiznet_header->valid & 0x80))
		encrypt((wiznet_header->valid & 0x7F), (uint8_t *)&reply, sizeof(WIZnet_Factory_Reset_Reply));

	ptr = 0;
	memcpy(buffer, wiznet_header, sizeof(WIZnet_Header));
	ptr += sizeof(WIZnet_Header);
	memcpy(buffer + ptr, (void *)&reply, sizeof(WIZnet_Factory_Reset_Reply));
	ptr += sizeof(WIZnet_Factory_Reset_Reply);

	if(wiznet_header->unicast == 0) {
		memset(remote_info->ip, 255, 4);
	}
	sendto(sock, buffer, ptr, remote_info->ip, remote_info->port);

	// Reboot to bootloader
	delay_cnt(100);
	NVIC_SystemReset();
}
Exemplo n.º 7
0
static void s2e_sockestablished_process(uint8_t sock)
{
	uint8_t tmp;
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;

	if(reconn_flag)
		reconn_flag = reconn_time = 0;

	switch(net->working_mode) {
		case TCP_MIXED_MODE:
		case TCP_SERVER_MODE:
			if(auth_flag) {
				auth_process(sock);
				return;
			}
		case TCP_CLIENT_MODE:
			if((inactive_flag == 0) && net->inactivity)
				inactive_flag = 1;
			else if(inactive_flag == 2) {
				inactive_flag = 0;
				disconnect(sock);
			}

			if(keepsend_flag == 0)
			{
				keepsend_flag = 1;
			}
			else if(keepsend_flag == 2)
			{
				keepsend_flag = 0;

				ctlwizchip(CW_GET_PHYLINK, (void*) &tmp);
				if(tmp == PHY_LINK_OFF)
				{
					//disconnect(sock); //TODO
				}
			}
			ether_to_uart(sock);
			uart_to_ether(sock);
			break;

		case UDP_MODE:
			close(sock);
			break;

		default:
			break;
	}
}
Exemplo n.º 8
0
static void s2e_sockclosewait_process(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;

	switch(net->working_mode) {
		case TCP_CLIENT_MODE:
		case TCP_SERVER_MODE:
		case TCP_MIXED_MODE:
		case UDP_MODE:
			disconnect(sock);
			close(sock);
			break;

		default:
			break;
	}
}
static void process_get_info(uint8_t sock, Remote_Info *remote_info, WIZnet_Header *wiznet_header)
{
	uint16_t len;
	uint8_t buffer[sizeof(WIZnet_Header) + sizeof(WIZnet_Get_Info_Reply)];
	uint32_t ptr;
	WIZnet_Get_Info get_info;
	WIZnet_Get_Info_Reply reply;
	S2E_Packet *s2e_packet = get_S2E_Packet_pointer();
	uint8_t ip[4];
	uint16_t port;

	getsockopt(sock, SO_REMAINSIZE, &len);
	if(len != wiznet_header->length)
		return;

	recvfrom(sock, (uint8_t *)&get_info, sizeof(WIZnet_Get_Info), ip, &port);

	if((wiznet_header->valid & 0x80))
		decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&get_info, len);

	if(memcmp(get_info.dst_mac_address, s2e_packet->network_info_common.mac, 6))
		return;

	// reply
	wiznet_header->length = sizeof(WIZnet_Get_Info_Reply);
	wiznet_header->op_code[1] = WIZNET_REPLY;

	memcpy(reply.src_mac_address, s2e_packet->network_info_common.mac, 6);
	memcpy(&reply.system_info, s2e_packet, sizeof(S2E_Packet));

	if((wiznet_header->valid & 0x80))
		encrypt((wiznet_header->valid & 0x7F), (uint8_t *)&reply, sizeof(WIZnet_Get_Info_Reply));

	ptr = 0;
	memcpy(buffer, wiznet_header, sizeof(WIZnet_Header));
	ptr += sizeof(WIZnet_Header);
	memcpy(buffer + ptr, (void *)&reply, sizeof(WIZnet_Get_Info_Reply));
	ptr += sizeof(WIZnet_Get_Info_Reply);

	if(wiznet_header->unicast == 0) {
		memset(remote_info->ip, 255, 4);
	}
	sendto(sock, buffer, ptr, remote_info->ip, remote_info->port);
}
void Net_Conf()
{
	S2E_Packet *value = get_S2E_Packet_pointer();
	wiz_NetInfo gWIZNETINFO;

	/* wizchip netconf */
	get_S2E_Packet_value(gWIZNETINFO.mac, value->network_info_common.mac, sizeof(gWIZNETINFO.mac[0]) * 6);
	get_S2E_Packet_value(gWIZNETINFO.ip, value->network_info_common.local_ip, sizeof(gWIZNETINFO.ip[0]) * 4);
	get_S2E_Packet_value(gWIZNETINFO.gw, value->network_info_common.gateway, sizeof(gWIZNETINFO.gw[0]) * 4);
	get_S2E_Packet_value(gWIZNETINFO.sn, value->network_info_common.subnet, sizeof(gWIZNETINFO.sn[0]) * 4);
	get_S2E_Packet_value(gWIZNETINFO.dns, value->options.dns_server_ip, sizeof(gWIZNETINFO.dns));
	if(value->options.dhcp_use)
		gWIZNETINFO.dhcp = NETINFO_DHCP;
	else
		gWIZNETINFO.dhcp = NETINFO_STATIC;

	ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);

//	display_Net_Info();
}
Exemplo n.º 11
0
static void s2e_sockudp_process(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;

	switch(net->working_mode) {
		case TCP_CLIENT_MODE:
		case TCP_SERVER_MODE:
		case TCP_MIXED_MODE:
			close(sock);
			break;

		case UDP_MODE:
			/* S2E ?�작 */
			ether_to_uart(sock);
			uart_to_ether(sock);
			break;

		default:
			break;
	}
}
Exemplo n.º 12
0
static void s2e_sockinit_process(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;
	uint8_t remote_ip[4];

	memcpy(remote_ip, net->remote_ip, sizeof(remote_ip));

	switch(net->working_mode) {
		case TCP_CLIENT_MODE:
			if(!reconn_flag) {
				connect(sock, remote_ip, net->remote_port);
				reconn_flag = 1;
			}

			break;

		case TCP_SERVER_MODE:
			listen(sock);
			break;
		case TCP_MIXED_MODE:
			if(RingBuffer_IsEmpty(&rxring)) {
				listen(sock);
				mixed_state = MIXED_SERVER;
			} else {
				if(!reconn_flag) {
					connect(sock, remote_ip, net->remote_port);
					reconn_flag = 1;
				}
				mixed_state = MIXED_CLIENT;
			}
			break;

		case UDP_MODE:
			close(sock);
			break;

		default:
			break;
	}
}
Exemplo n.º 13
0
static void auth_process(uint8_t sock)
{
	struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options);
	uint8_t sock_state;
	uint16_t len = 0, pw_len = 0;
	static uint16_t prev_len = 0;

	pw_len = strlen(option->pw_connect);

	getsockopt(sock, SO_STATUS, &sock_state);

	if(sock_state == SOCK_ESTABLISHED) {
		getsockopt(sock, SO_RECVBUF, &len);

		if(len != prev_len) {
			auth_time = 0;
			prev_len = len;
		}

		if(auth_time >= 5) {
			disconnect(sock);
			auth_flag = auth_time = 0;
			return;
		}

		if(len >= pw_len) {
			len = recv(sock, g_recv_buf, pw_len);
			if(len != pw_len) {
				disconnect(sock);
				auth_flag = auth_time = 0;
				return;
			}
			
			if(memcmp(option->pw_connect, g_recv_buf, pw_len))		// Password Fault
				disconnect(sock);

			auth_flag = auth_time = 0;
		}
	}
}
void reply_firmware_upload_done(uint8_t sock)
{
	uint8_t buffer[sizeof(WIZnet_Header) + sizeof(WIZnet_Firmware_Upload_Done_Reply)];
	uint32_t ptr;
	WIZnet_Firmware_Upload_Done_Reply reply;
	Firmware_Upload_Info firmware_upload_info;
	S2E_Packet *s2e_packet = get_S2E_Packet_pointer();
	uint8_t ip[4];
	uint16_t port;

	close(sock);
	socket(sock, Sn_MR_UDP, REMOTE_CLIENT_PORT, 0);

	read_storage(0, &firmware_upload_info, sizeof(Firmware_Upload_Info));

	// reply
	firmware_upload_info.wiznet_header.length = sizeof(WIZnet_Firmware_Upload_Done_Reply);
	firmware_upload_info.wiznet_header.op_code[0] = FIRMWARE_UPLOAD_DONE;
	firmware_upload_info.wiznet_header.op_code[1] = WIZNET_REPLY;

	memcpy(reply.src_mac_address, s2e_packet->network_info_common.mac, 6);

	if((firmware_upload_info.wiznet_header.valid & 0x80))
		encrypt((firmware_upload_info.wiznet_header.valid & 0x7F), (uint8_t *)&reply, sizeof(WIZnet_Firmware_Upload_Done_Reply));

	ptr = 0;
	memcpy(buffer, &firmware_upload_info.wiznet_header, sizeof(WIZnet_Header));
	ptr += sizeof(WIZnet_Header);
	memcpy(buffer + ptr, (void *)&reply, sizeof(WIZnet_Firmware_Upload_Done_Reply));
	ptr += sizeof(WIZnet_Firmware_Upload_Done_Reply);

	if(firmware_upload_info.wiznet_header.unicast == 0) {
		memset(firmware_upload_info.configtool_info.ip, 255, 4);
	}
	memcpy(ip, firmware_upload_info.configtool_info.ip, sizeof(uint8_t) * 4);
	memcpy(&port, &firmware_upload_info.configtool_info.port, sizeof(uint16_t));
	sendto(sock, buffer, ptr, ip, port);
}
Exemplo n.º 15
0
void UART_Init(void)
{
	struct __serial_info *serial = (struct __serial_info *)(get_S2E_Packet_pointer()->serial_info);

	/* Setup UART for 115.2K8N1 */
	Chip_UART_Init(LPC_USART);

	serial_info_init(serial);
	Chip_UART_SetupFIFOS(LPC_USART, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV3));
	Chip_UART_TXEnable(LPC_USART);

	/* Before using the ring buffers, initialize them using the ring
	   buffer init function */
	RingBuffer_Init(&rxring, rxbuff, 1, UART_RRB_SIZE);
	RingBuffer_Init(&txring, txbuff, 1, UART_SRB_SIZE);

	/* Enable receive data and line status interrupt */
	Chip_UART_IntEnable(LPC_USART, (UART_IER_RBRINT | UART_IER_RLSINT));

	/* preemption = 1, sub-priority = 1 */
	NVIC_SetPriority(UART0_IRQn, 1);
	NVIC_EnableIRQ(UART0_IRQn);
}
Exemplo n.º 16
0
static void s2e_sockclose_process(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;

	switch(net->working_mode) {
		case TCP_CLIENT_MODE:
		case TCP_SERVER_MODE:
		case TCP_MIXED_MODE:
			//socket(sock, Sn_MR_TCP, net->local_port, 0);
			socket(sock, Sn_MR_TCP, net->local_port, SF_IO_NONBLOCK);
			//printf("TCP Socket\r\n");
			break;

		case UDP_MODE:
			//socket(sock, Sn_MR_UDP, net->local_port, 0);
			socket(sock, Sn_MR_UDP, net->local_port, SF_IO_NONBLOCK);
			//printf("UDP Socket\r\n");
			break;

		default:
			break;
	}
}
Exemplo n.º 17
0
static void trigger_ready_process()
{
	struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options);

	if((pattern_cnt = RingBuffer_SerachPattern(&rxring, pattern_offset, option->serial_trigger, 1))) {
		pattern_offset = pattern_cnt;
		trigger_state = TRIG_STATE_1;
#ifdef __TRIG_DEBUG__
		printf("TRIG_STATE_1\r\n");
#endif
		trigger_time = uart_size_prev = 0;
		trigger_flag = 1;
		return;
	} else
		ready_cnt = RingBuffer_GetCount(&rxring);
		
	if(uart_size_prev != RingBuffer_GetCount(&rxring)) {
		trigger_state = TRIG_STATE_NONE;
#ifdef __TRIG_DEBUG__
		printf("[%s] TRIG_STATE_NONE\r\n", __func__);
#endif
		trigger_flag = trigger_time = uart_size_prev = 0;
	}
}
Exemplo n.º 18
0
static void s2e_socklisten_process(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;

	switch(net->working_mode) {
		case TCP_CLIENT_MODE:
			close(sock);
			break;

		case TCP_SERVER_MODE:
			break;

		case TCP_MIXED_MODE:
			if(!RingBuffer_IsEmpty(&rxring))
				close(sock);
			break;
		case UDP_MODE:
			close(sock);
			break;

		default:
			break;
	}
}
static void process_discovery(uint8_t sock, Remote_Info *remote_info, WIZnet_Header *wiznet_header)
{
	uint16_t len;
	uint8_t buffer[sizeof(WIZnet_Header) + sizeof(WIZnet_Discovery_Mixed_Condition)];	// header + most biggest structure
	uint32_t ptr;
	WIZnet_Discovery_Reply reply;
	S2E_Packet *s2e_packet = get_S2E_Packet_pointer();
	uint8_t ip[4];
	uint16_t port;

	getsockopt(sock, SO_REMAINSIZE, &len);
	if(len != wiznet_header->length)
		return;

	switch(wiznet_header->op_code[0]) {
	case DISCOVERY_ALL:
		break;

	case DISCOVERY_PRODUCT_CODE:
	{
		WIZnet_Discovery_Product_Code product_code;
		recvfrom(sock, (uint8_t *)&product_code, sizeof(WIZnet_Discovery_Product_Code), ip, &port);

		if((wiznet_header->valid & 0x80))
			decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&product_code, len);

		if(memcmp(product_code.product_code, s2e_packet->module_type, 3))
			return;
		break;
	}

	case DISCOVERY_MAC_ADDRESS:
	{
		WIZnet_Discovery_MAC_Address mac_address;
		recvfrom(sock, (uint8_t *)&mac_address, sizeof(WIZnet_Discovery_MAC_Address), ip, &port);

		if((wiznet_header->valid & 0x80))
			decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&mac_address, len);

		if(   mac_address.start_mac_address[0] > s2e_packet->network_info_common.mac[0]
		   || mac_address.start_mac_address[1] > s2e_packet->network_info_common.mac[1]
		   || mac_address.start_mac_address[2] > s2e_packet->network_info_common.mac[2]
		   || mac_address.start_mac_address[3] > s2e_packet->network_info_common.mac[3]
		   || mac_address.start_mac_address[4] > s2e_packet->network_info_common.mac[4]
		   || mac_address.start_mac_address[5] > s2e_packet->network_info_common.mac[5])
			return;
		if(   mac_address.end_mac_address[0] < s2e_packet->network_info_common.mac[0]
		   || mac_address.end_mac_address[1] < s2e_packet->network_info_common.mac[1]
		   || mac_address.end_mac_address[2] < s2e_packet->network_info_common.mac[2]
		   || mac_address.end_mac_address[3] < s2e_packet->network_info_common.mac[3]
		   || mac_address.end_mac_address[4] < s2e_packet->network_info_common.mac[4]
		   || mac_address.end_mac_address[5] < s2e_packet->network_info_common.mac[5])
			return;
		break;
	}

	case DISCOVERY_ALIAS:
	{
		WIZnet_Discovery_Alias alias;
		recvfrom(sock, (uint8_t *)&alias, sizeof(WIZnet_Discovery_Alias), ip, &port);

		if((wiznet_header->valid & 0x80))
			decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&alias, len);

		if(memcmp(alias.alias, s2e_packet->module_name, strlen((char *)alias.alias)))
			return;
		break;
	}

	case DISCOVERY_MIXED_COND:
	{
		WIZnet_Discovery_Mixed_Condition mixed_condition;
		recvfrom(sock, (uint8_t *)&mixed_condition, sizeof(WIZnet_Discovery_Mixed_Condition), ip, &port);

		if((wiznet_header->valid & 0x80))
			decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&mixed_condition, len);

		break;
	}

	default:
		return;
	}

	// reply
	wiznet_header->length = sizeof(WIZnet_Discovery_Reply);
	wiznet_header->op_code[1] = WIZNET_REPLY;

	memcpy(reply.product_code, s2e_packet->module_type, 3);
	memcpy(reply.fw_version, s2e_packet->fw_ver, 3);
	memcpy(reply.mac_address, s2e_packet->network_info_common.mac, 6);

	if((wiznet_header->valid & 0x80))
		encrypt((wiznet_header->valid & 0x7F), (uint8_t *)&reply, sizeof(WIZnet_Discovery_Reply));

	ptr = 0;
	memcpy(buffer, wiznet_header, sizeof(WIZnet_Header));
	ptr += sizeof(WIZnet_Header);
	memcpy(buffer + ptr, (void *)&reply, sizeof(WIZnet_Discovery_Reply));
	ptr += sizeof(WIZnet_Discovery_Reply);

	if(wiznet_header->unicast == 0) {
		memset(remote_info->ip, 255, 4);
	}
	sendto(sock, buffer, ptr, remote_info->ip, remote_info->port);
}
static void process_firmware_upload_init(uint8_t sock, Remote_Info *remote_info, WIZnet_Header *wiznet_header)
{
	S2E_Packet *value = get_S2E_Packet_pointer();
	struct __network_info_common tmp;
	uint16_t len;
	WIZnet_Firmware_Upload_Init firmware_upload;
	uint8_t buffer[sizeof(WIZnet_Header) + sizeof(WIZnet_Firmware_Upload_Done_Reply)];
	uint32_t ptr;
	WIZnet_Firmware_Upload_Init_Reply reply;
	Firmware_Upload_Info firmware_upload_info;
	S2E_Packet *s2e_packet = get_S2E_Packet_pointer();
	uint8_t ip[4];
	uint16_t port;

	getsockopt(sock, SO_REMAINSIZE, &len);
	if(len != wiznet_header->length)
		return;

	recvfrom(sock, (uint8_t *)&firmware_upload, sizeof(WIZnet_Firmware_Upload_Init), ip, &port);

	if(wiznet_header->op_code[0] != FIRMWARE_UPLOAD_INIT)
		return;

	if((wiznet_header->valid & 0x80))
		decrypt((wiznet_header->valid & 0x7F), (uint8_t *)&firmware_upload, len);

	if(memcmp(firmware_upload.dst_mac_address, s2e_packet->network_info_common.mac, 6))
		return;
	
	if(strcmp((char *)firmware_upload.set_pw, (char *)s2e_packet->options.pw_setting))
		return;

	/* Storee Current Network Information (For DHCP) */
	memcpy(&tmp, &(value->network_info_common), sizeof(struct __network_info_common));
	load_S2E_Packet_from_storage();
	memcpy(&(value->network_info_common), &tmp, sizeof(struct __network_info_common));
	save_S2E_Packet_to_storage();

	//write the TFTP server ip, port, filename and etc. to storage
	memcpy(&firmware_upload_info.wiznet_header                , wiznet_header                            , sizeof(WIZnet_Header));
	memcpy(&firmware_upload_info.configtool_info              , remote_info                              , sizeof(Remote_Info));
	memcpy(firmware_upload_info.tftp_info.ip                  , (uint8_t *) (firmware_upload.server_ip)  , sizeof(uint8_t)*4);
	memcpy((uint8_t *)&(firmware_upload_info.tftp_info.port)  , (uint8_t *)&(firmware_upload.server_port), sizeof(uint8_t)*2);
	memcpy(firmware_upload_info.filename                      , (uint8_t *) (firmware_upload.file_name)  , sizeof(uint8_t)*50);
	write_storage(0, &firmware_upload_info, sizeof(Firmware_Upload_Info));

	// reply
	wiznet_header->length = sizeof(WIZnet_Firmware_Upload_Init_Reply);
	wiznet_header->op_code[1] = WIZNET_REPLY;

	memcpy(reply.src_mac_address, s2e_packet->network_info_common.mac, 6);

	if((wiznet_header->valid & 0x80))
		encrypt((wiznet_header->valid & 0x7F), (uint8_t *)&reply, sizeof(WIZnet_Firmware_Upload_Init_Reply));

	ptr = 0;
	memcpy(buffer, wiznet_header, sizeof(WIZnet_Header));
	ptr += sizeof(WIZnet_Header);
	memcpy(buffer + ptr, (void *)&reply, sizeof(WIZnet_Firmware_Upload_Init_Reply));
	ptr += sizeof(WIZnet_Firmware_Upload_Init_Reply);

	if(wiznet_header->unicast == 0) {
		memset(remote_info->ip, 255, 4);
	}
	sendto(sock, buffer, ptr, remote_info->ip, remote_info->port);

	// Reboot to bootloader
	delay_cnt(100);
	NVIC_SystemReset();
}
Exemplo n.º 21
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main()
{
    //uint8_t tx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };
    //uint8_t rx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };
    //uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0x33};
    //uint8_t src_addr[4] = {192, 168,  0,  80};
    //uint8_t gw_addr[4]  = {192, 168,  0,  1};
    //uint8_t sub_addr[4] = {255, 255, 255,  0};
    //uint8_t dns_server[4] = {8, 8, 8, 8};           // for Example domain name server
    //uint8_t tmp[8];
	//int ret;
	int i;
#if defined (_MAIN_DEBUG_) && defined (_USE_SDCARD_)
	int ret;
#endif
#if defined(F_APP_FTP)
	wiz_NetInfo gWIZNETINFO;
#endif
#if defined(F_APP_DHCP) || defined(F_APP_DNS)
	S2E_Packet *value = get_S2E_Packet_pointer();
#endif
#if defined(F_APP_DNS)
	uint8_t dns_server_ip[4];
#endif

    /* External Clock */
    CRG_PLL_InputFrequencySelect(CRG_OCLK);

    /* Set System init */
    SystemInit();

    /* UART Init */
    UART_StructInit(&UART_InitStructure);
    UART_Init(UART_DEBUG,&UART_InitStructure);

    /* SysTick_Config */
    SysTick_Config((GetSystemClock()/1000));

    /* Set WZ_100US Register */
    setTIC100US((GetSystemClock()/10000));
    //getTIC100US();	
    //printf(" GetSystemClock: %X, getTIC100US: %X, (%X) \r\n", 
    //      GetSystemClock, getTIC100US(), *(uint32_t *)TIC100US);        

	LED_Init(LED1);
	LED_Init(LED2);
	LED_Init(LED3);

	LED_Off(LED1);
	LED_Off(LED2);
	LED_Off(LED3);

	g_sdcard_done = 0;

	BOOT_Pin_Init();
	Board_factory_Init();
	EXTI_Configuration();

	/* Load Configure Information */
	load_S2E_Packet_from_storage();
	UART_Configuration();

	/* Check MAC Address */
	check_mac_address();

	Timer0_Configuration();

    // ADC initialize
    ADC_Init();

#ifdef _MAIN_DEBUG_
	uint8_t tmpstr[6] = {0,};

	ctlwizchip(CW_GET_ID,(void*)tmpstr);
    printf("\r\n============================================\r\n");
	printf(" WIZnet %s EVB Demo v%d.%.2d\r\n", tmpstr, VER_H, VER_L);
	printf("============================================\r\n");
	printf(" WIZwiki Platform based WEBBoot Example\r\n");
	printf("============================================\r\n");
#endif

#ifdef __DEF_USED_IC101AG__ //For using IC+101AG
    *(volatile uint32_t *)(0x41003068) = 0x64; //TXD0 - set PAD strengh and pull-up
    *(volatile uint32_t *)(0x4100306C) = 0x64; //TXD1 - set PAD strengh and pull-up
    *(volatile uint32_t *)(0x41003070) = 0x64; //TXD2 - set PAD strengh and pull-up
    *(volatile uint32_t *)(0x41003074) = 0x64; //TXD3 - set PAD strengh and pull-up
    *(volatile uint32_t *)(0x41003050) = 0x64; //TXE  - set PAD strengh and pull-up
#endif

#ifdef __DEF_USED_MDIO__ 
    /* mdio Init */
    mdio_init(GPIOB, MDC, MDIO );
    /* PHY Link Check via gpio mdio */
    while( link() == 0x0 )
    {
        printf(".");  
        delay(500);
    }
    printf("PHY is linked. \r\n");  
#else
    delay(1000);
    delay(1000);
#endif

	Mac_Conf();
#if defined(F_APP_DHCP)
	if(value->options.dhcp_use)		// DHCP
	{
		uint32_t ret;
		uint8_t dhcp_retry = 0;

#ifdef _MAIN_DEBUG_
		printf(" - DHCP Client running\r\n");
#endif

		DHCP_init(SOCK_DHCP, TX_BUF);
		reg_dhcp_cbfunc(w5500_dhcp_assign, w5500_dhcp_assign, w5500_dhcp_conflict);

		while(1)
		{
			ret = DHCP_run();

			if(ret == DHCP_IP_LEASED)
			{
#ifdef _MAIN_DEBUG_
				printf(" - DHCP Success: DHCP Leased time : %ld Sec.\r\n\r\n", getDHCPLeasetime());
#endif
				break;
			}
			else if(ret == DHCP_FAILED)
			{
				dhcp_retry++;
#ifdef _MAIN_DEBUG_
				if(dhcp_retry <= 3) printf(" - DHCP Timeout occurred and retry [%d]\r\n", dhcp_retry);
#endif
			}

			if(dhcp_retry > 3)
			{
#ifdef _MAIN_DEBUG_
				printf(" - DHCP Failed\r\n\r\n");
#endif
				value->options.dhcp_use = 0;
				Net_Conf();
				break;
			}

			do_udp_config(SOCK_CONFIG);
		}
	}
	else 								// Static
	{
		Net_Conf();
	}
#else
	Net_Conf();
#endif

#ifdef _MAIN_DEBUG_
	display_Net_Info();
#endif

#if defined(F_APP_ATC)
	atc_init(&rxring, &txring);

	op_mode = OP_DATA;
#endif

	TFTP_init(SOCK_TFTP, socket_buf);

	ret = application_update();

    printf("[DEBUG] check trigger:%d ret:%d \r\n", get_bootpin_Status(), ret);
	if((get_bootpin_Status() == 1) && (ret != TFTP_FAIL)) {
		uint32_t tmp;

#if !defined(MULTIFLASH_ENABLE)
		tmp = *(volatile uint32_t *)APP_BASE;
#else
		tmp = *(volatile uint32_t *)flash.flash_app_base;
#endif

		if((tmp & 0xffffffff) != 0xffffffff) {
		    printf("[DEBUG] application_jump\r\n");
			application_jump();
		}
	}

#ifdef _USE_SDCARD_
	// SD card Initialization
	ret = mmc_mount();
	if(ret <= 0)
	{
#ifdef _MAIN_DEBUG_
		printf("\r\n - Can't mount SD card: Please Reboot WIZ750WEB Board or Insert SD card\r\n");
#endif
		//while(!(ret = mmc_mount()));
	}

	if(ret > 0)
	{
#ifdef _MAIN_DEBUG_
		display_SDcard_Info(ret);
#endif
	}
#endif

	httpServer_init(TX_BUF, RX_BUF, MAX_HTTPSOCK, socknumlist);
#ifdef _USE_WATCHDOG_
	reg_httpServer_cbfunc(NVIC_SystemReset, IWDG_ReloadCounter); // Callback: STM32 MCU Reset / WDT Reset (IWDG)
#else
	reg_httpServer_cbfunc(NVIC_SystemReset, NULL); // Callback: STM32 MCU Reset
#endif
	IO_status_init();

#if defined(F_APP_FTP)
	ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
	ftpd_init(gWIZNETINFO.ip);	// Added by James for FTP
#endif

#ifdef _USE_WATCHDOG_
	// IWDG Initialization: STM32 Independent WatchDog
	IWDG_Configureation();
#endif

	while (1) {
#ifdef _USE_WATCHDOG_
		IWDG_ReloadCounter(); // Feed IWDG
#endif

#if defined(F_APP_ATC)
		atc_run();
#endif
		
		if(g_op_mode == NORMAL_MODE) {
			do_udp_config(SOCK_CONFIG);
		} else {
			if(TFTP_run() != TFTP_PROGRESS)
				g_op_mode = NORMAL_MODE;
		}

#if defined(F_APP_DHCP)
		if(value->options.dhcp_use)
			DHCP_run();
#endif

		for(i = 0; i < MAX_HTTPSOCK; i++)	httpServer_run(i);	// HTTP server handler

#if defined(F_APP_FTP)
		ftpd_run(FTP_DBUF);
#endif

#ifdef _USE_WATCHDOG_
		IWDG_ReloadCounter(); // Feed IWDG
#endif

	}

    return 0;
}
Exemplo n.º 22
0
static void uart_to_ether(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;
	uint8_t sock_state, snd_flag = 0, mask_bit = 0;
	uint16_t len = 0;
	int ret, uart_read_len = sizeof(g_send_buf);

	if(RingBuffer_IsEmpty(&rxring))
		return;

	/* Serial Trigger Process */
	if((trigger_state != TRIG_STATE_NONE) && (trigger_state != TRIG_STATE_READY))
		return;

	/* UART Packing Size Check */
	if(net->packing_size)
		mask_bit |= 0x01;

	if(RingBuffer_GetCount(&rxring) < net->packing_size)
		snd_flag |= 0x01;
	else {
		if(net->packing_size)
			uart_read_len = net->packing_size;
	}

	/* Nagle Timer Process */
	if(net->packing_time)
		mask_bit |= 0x02;

	if(!nagle_flag && net->packing_time)
		snd_flag |= 0x02;

	/* Seperator Process */
	if(net->packing_delimiter_length)  {
		mask_bit |= 0x04;
		if(!(ret = RingBuffer_SerachPattern(&rxring, 0, net->packing_delimiter, net->packing_delimiter_length)))
			snd_flag |= 0x04;
		else {
			if(!(mask_bit & 0x01) || (snd_flag & 0x01) || (ret + net->packing_data_appendix < net->packing_size)) {
				uart_read_len = ret + net->packing_data_appendix;

				if(RingBuffer_GetCount(&rxring) < uart_read_len)
					snd_flag |= 0x04;
			}
		}
	}

	//printf("snd_flag = %02x / mask_bit = %02x\r\n", snd_flag, mask_bit);
	if(mask_bit != 0)				// mask_bit : Enable Uart Packing Condition bit
		if((snd_flag & mask_bit) == mask_bit)
			return;

	/* Serial Trigger Process */
	if(trigger_state == TRIG_STATE_READY)
		if(ready_cnt != RingBuffer_GetCount(&rxring))
			return;

	getsockopt(sock, SO_STATUS, &sock_state);

	if(sock_state != SOCK_UDP && sock_state != SOCK_ESTABLISHED)
		return;

	/* Data Transfer */
	len = Chip_UART_ReadRB(UART_DATA, &rxring, &g_send_buf, uart_read_len);
	if(len < 0) {
		//printf("uart recv error\r\n");
		return;
	}
	uart_recv_cnt += len;
	pattern_offset = 0;

	if(sock_state == SOCK_UDP) {
		uint8_t remote_ip[4];
		memcpy(remote_ip, net->remote_ip, sizeof(remote_ip));
		ret = sendto(sock, g_send_buf, len, remote_ip, net->remote_port);
		if(ret != len) {
			//printf("sendto error\r\n");
			return;
		}
		ether_send_cnt += len;
	} else if(sock_state == SOCK_ESTABLISHED) {
		ret = send(sock, g_send_buf, len);
		if(ret != len) {
			//printf("send error\r\n");
			return;
		}
		ether_send_cnt += len;
	}

	nagle_flag = nagle_time = uart_recv_count = 0;
}
Exemplo n.º 23
0
void make_basic_config_setting_json_callback(uint8_t * buf, uint16_t * len)
{
	S2E_Packet *value = get_S2E_Packet_pointer();
	uint8_t baudrate_index[2] = {0, };
	uint8_t databit_index[2] = {0, };
	uint8_t stopbit_index[2] = {0, };
	uint8_t i;

	for(i = 0; i < 2; i++)
	{
		switch(value->serial_info[i].baud_rate)
		{
			case baud_230400 :
				baudrate_index[i] = BAUD_230400_INDEX;
				break;
			case baud_115200 :
				baudrate_index[i] = BAUD_115200_INDEX;
				break;
			case baud_57600 :
				baudrate_index[i] = BAUD_57600_INDEX;
				break;
			case baud_38400 :
				baudrate_index[i] = BAUD_38400_INDEX;
				break;
			case baud_19200 :
				baudrate_index[i] = BAUD_19200_INDEX;
				break;
			case baud_9600 :
				baudrate_index[i] = BAUD_9600_INDEX;
				break;
			case baud_4800 :
				baudrate_index[i] = BAUD_4800_INDEX;
				break;
			case baud_2400 :
				baudrate_index[i] = BAUD_2400_INDEX;
				break;
			case baud_1200 :
				baudrate_index[i] = BAUD_1200_INDEX;
				break;
			case baud_600 :
				baudrate_index[i] = BAUD_600_INDEX;
				break;
			default :
				baudrate_index[i] = BAUD_115200_INDEX;
				break;
		}

		if(value->serial_info[i].data_bits == word_len9) databit_index[i] = DATA_BIT9_INDEX;
		else databit_index[i] = DATA_BIT8_INDEX;

		if(value->serial_info[i].stop_bits == stop_bit2) stopbit_index[i] = STOP_BIT2_INDEX;
		else stopbit_index[i] = STOP_BIT1_INDEX;
	}

	*len = sprintf((char *)buf, "BasicSettingCallback({\"ver\":\"%d.%d.%d\",\
                                        \"dname\":\"%s\",\
                                        \"pcode\":\"%d-%d-%d\",\
                                        \"mac\":\"%02X:%02X:%02X:%02X:%02X:%02X\",\
                                        \"dhcp\":\"%d\",\
                                        \"lip\":\"%d.%d.%d.%d\",\
                                        \"gw\":\"%d.%d.%d.%d\",\
                                        \"sub\":\"%d.%d.%d.%d\",\
                                        \"dns\":\"%d.%d.%d.%d\",\
                                        \"baud1\":\"%d\",\
                                        \"databit1\":\"%d\",\
                                        \"parity1\":\"%d\",\
                                        \"stopbit1\":\"%d\",\
                                        \"flow1\":\"%d\",\
										\"baud2\":\"%d\",\
                                        \"databit2\":\"%d\",\
                                        \"parity2\":\"%d\",\
                                        \"stopbit2\":\"%d\",\
                                        \"flow2\":\"%d\"\
                                        });",value->fw_ver[0], value->fw_ver[1], value->fw_ver[2],
Exemplo n.º 24
0
void s2e_run(uint8_t sock)
{
	struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;
	struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options);
	uint8_t sock_state;

	getsockopt(sock, SO_STATUS, &sock_state);

#ifdef __TRIG_DEBUG__
	if(tmp1 != trigger_state) {
//		printf("trigger state : %d\r\n", trigger_state);
//		printf("Uart Prev Size : %d\r\n\r\n", uart_size_prev);
		printf("Pattern Offset : %d\r\n", pattern_offset);
		tmp1 = trigger_state;
	}
#endif

	/* Serial Trigger Process */
	switch(trigger_state) {
		case TRIG_STATE_NONE:
			trigger_none_process(sock_state);
			break;
		
		case TRIG_STATE_READY:
			trigger_ready_process();
			break;

		case TRIG_STATE_1:
			trigger_state1_process();
			break;

		case TRIG_STATE_2:
			trigger_state2_process();
			break;

		case TRIG_STATE_3:
			trigger_state3_process(sock);
			if(op_mode == OP_COMMAND)
				return;
			break;

		default:
			break;
	}

	/* Network State Process */
	switch(net->state) {
		case net_disconnect:
			if(sock_state == SOCK_UDP)
				net->state = net_udp;
			else if(sock_state == SOCK_ESTABLISHED) {
				net->state = net_connect;
				LED_On(LED3);

				if(option->pw_connect[0] != 0) {
					if(net->working_mode == TCP_MIXED_MODE) {
						if(mixed_state == MIXED_SERVER)
							auth_flag = 1;
					} else
						auth_flag = 1;
				}
			}
			break;

		case net_connect:
			if(sock_state != SOCK_ESTABLISHED) {
				net->state = net_disconnect;
				LED_Off(LED3);

				mixed_state = MIXED_SERVER;
			}
			break;

		case net_udp:
			if(sock_state != SOCK_UDP) {
				net->state = net_disconnect;
				LED_Off(LED3);
			}
			break;

		default:
			net->state = net_disconnect;
			LED_Off(LED3);
			break;
	}

	switch (sock_state) {
		case SOCK_CLOSED:
			s2e_sockclose_process(sock);
			break;

		case SOCK_INIT:
			s2e_sockinit_process(sock);
			break;

		case SOCK_LISTEN:
			s2e_socklisten_process(sock);
			break;

		case SOCK_ESTABLISHED:
			s2e_sockestablished_process(sock);
			break;

		case SOCK_CLOSE_WAIT:
			s2e_sockclosewait_process(sock);
			break;

		case SOCK_UDP:
			s2e_sockudp_process(sock);
			break;

		default:
			break;
	}
}
void set_static_mode()
{
	S2E_Packet *value = get_S2E_Packet_pointer();

	value->options.dhcp_use = 0;
}
void set_dhcp_mode()
{
	S2E_Packet *value = get_S2E_Packet_pointer();

	value->options.dhcp_use = 1;
}
void Mac_Conf()
{
	S2E_Packet *value = get_S2E_Packet_pointer();
	setSHAR(value->network_info_common.mac);
}
Exemplo n.º 28
0
void DUALTIMER0_Handler(void)
{
    struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info;
    struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options);
    uint32_t count = 0;

    if(DUALTIMER_GetIntStatus(DUALTIMER0_0))
    {
        DUALTIMER_IntClear(DUALTIMER0_0);

        mill_cnt++;

        /* UART Packing Timer Process */
        if(!nagle_flag && net->packing_time) {
            count = RingBuffer_GetCount(&rxring);

            if(count != 0 && count == uart_recv_count)
                nagle_time++;
            else {
                nagle_time = 0;
                uart_recv_count = count;
            }

            if(nagle_time >= net->packing_time) {
                nagle_flag = 1;
                nagle_time = 0;
            }
        }

        /* Reconnection Process */
        if(reconn_flag)
            reconn_time++;

        if(net->reconnection <= reconn_time) {
            reconn_flag = 0;
            reconn_time = 0;
        }

        /* Factory Reset Process */
        if(factory_flag) {
            factory_time++;

            if(Board_factory_get()) {
                factory_time = factory_flag = 0;
                NVIC_EnableIRQ(EXTI_IRQn);
            }

            if(factory_time >= 5000) {
                /* Factory Reset */
                set_S2E_Packet_to_factory_value();
                save_S2E_Packet_to_storage();

                NVIC_SystemReset();
            }
        }

        /* Serial Trigger Timer Process */
        if(trigger_flag == 1)
            trigger_time++;

        if(trigger_time >= 500) {
            trigger_flag = 2;
            trigger_time = 0;
        }

        /* Second Process */
        if((mill_cnt % 1000) == 0) {
            LED_Toggle(LED2);
            mill_cnt = 0;
            sec_cnt++;

            /* DHCP Process */
            if(option->dhcp_use)
                DHCP_time_handler();

            /* DNS Process */
            if(option->dns_use) {
                DNS_time_handler();
            }

            /* Inactive Time Process */
            if(inactive_flag == 1) {
                if((ether_recv_cnt == ether_recv_prev) && (ether_send_cnt == ether_send_prev))
                    inactive_time++;
                else {
                    ether_send_prev = ether_send_cnt;
                    ether_recv_prev = ether_recv_cnt;
                    inactive_time = 0;
                }
            }

            if(net->inactivity && (net->inactivity <= inactive_time)) {
                inactive_flag = 2;
                inactive_time = 0;
            }

            /* Connect Password Process */
            if(auth_flag)
                auth_time++;

            /* Minute Process */
            if((sec_cnt % 60) == 0) {
                sec_cnt = 0;

                /* DNS Process */
                if(option->dns_use) {
                    run_dns = 1;
                }
            }
        }

        mill_sec++;

        //if((mill_sec % 100) == 0) {
        //	LED_Toggle(LED1);
        //	LED_Toggle(LED2);
        //}

        if((mill_sec % 1000) == 0) {
            mill_sec = 0;
            tftp_timeout_handler();
        }

    }
}
Exemplo n.º 29
0
/**
 * Sockwatch Module Handler
 * If you use Sockwatch Module, this should run in the main loop
 * @ingroup sockwatch_module
 */
void sockwatch_run(void)
{
#define WCF_HANDLE(item_v, ret_v) \
do { \
	BITCLR(watch_sock[i], item_v); \
	watch_cb[i](i, item_v, ret_v); \
} while(0)

	uint8_t i;
	int32_t ret;

	S2E_Packet *value = get_S2E_Packet_pointer();
	MQTTPacket_connectData mqttConnectData = MQTTPacket_connectData_initializer;

	for(i=0; i<TOTAL_SOCK_NUM; i++) {
		if(watch_sock[i] == 0) continue;
		if(atci.mqtt_run == 1) {
			ret = MQTTYield(&mqttClient, mqttConnectData.keepAliveInterval);
#ifdef __ATC_MODE_MQTT_DEBUG__
			if(ret != SUCCESSS) {
				printf("MQTT yield error - ret : %d\r\n", ret);
			}
#endif
		}
		if(watch_sock[i] & WATCH_SOCK_RECV) {	// checked every time when 'connected' state
			getsockopt(i, SO_RECVBUF, (uint16_t*)&ret);
			if((uint16_t)ret > 0) WCF_HANDLE(WATCH_SOCK_RECV, RET_OK);
		}
		if(watch_sock[i] & WATCH_SOCK_CLS_EVT) {	// checked every time when 'connected' state
			getsockopt(i, SO_STATUS, (uint8_t*)&ret);
			switch((uint8_t)ret) {
			case SOCK_CLOSED:
				WCF_HANDLE(WATCH_SOCK_CLS_EVT, RET_OK);
				break;
			case SOCK_CLOSE_WAIT:
				disconnect(i);
				//close(i);
				break;
			default:
				break;
			}
		}
		if(watch_sock[i] & WATCH_SOCK_CONN_EVT) {	// checked every time when 'listen' state
			getsockopt(i, SO_STATUS, (uint8_t*)&ret);
			switch((uint8_t)ret) {
			case SOCK_ESTABLISHED:
				WCF_HANDLE(WATCH_SOCK_CONN_EVT, RET_OK);
				break;
			default:
				break;
			}
		}
		if((watch_sock[i] & WATCH_SOCK_MASK_LOW) == 0) continue;	// things which occurs occasionally will be checked all together
		if(watch_sock[i] & WATCH_SOCK_CLS_TRY) {
			getsockopt(i, SO_STATUS, (uint8_t*)&ret);
			switch((uint8_t)ret) {
			case SOCK_LISTEN:
				close(i);
			case SOCK_CLOSED:
				WCF_HANDLE(WATCH_SOCK_CLS_TRY, RET_OK);
				break;
			case SOCK_FIN_WAIT:
				close(i);	
				break;
			default:
				ctlsocket(i, CS_GET_INTERRUPT, (uint8_t*)&ret);
				if((uint8_t)ret & Sn_IR_TIMEOUT){
					//ctlsocket(i, CS_CLR_INTERRUPT, (uint8_t*)&ret);
					close(i);
					WCF_HANDLE(WATCH_SOCK_CLS_TRY, SOCKERR_TIMEOUT);
				}
				disconnect(i);
				//close(i);
				break;
			}
		}
		if(watch_sock[i] & WATCH_SOCK_CONN_TRY) {
			getsockopt(i, SO_STATUS, (uint8_t*)&ret);
			switch((uint8_t)ret) {
			case SOCK_ESTABLISHED:
				WCF_HANDLE(WATCH_SOCK_CONN_TRY, RET_OK);
				if(atci.mqtt_con == 1) {
					if(getSn_IR(i) & Sn_IR_CON) {
						setSn_IR(i,Sn_IR_CON);

						mqttConnectData.MQTTVersion			= 4;
						mqttConnectData.clientID.cstring 	= value->module_name;
						mqttConnectData.username.cstring 	= value->options.mqtt_user;
						mqttConnectData.password.cstring 	= value->options.mqtt_pw;
						mqttConnectData.willFlag 			= 0;
						mqttConnectData.keepAliveInterval   = 60;
						mqttConnectData.cleansession        = 1;

						ret = MQTTConnect(&mqttClient, &mqttConnectData);
#ifdef __ATC_MODE_MQTT_DEBUG__
						if(ret != SUCCESSS) {
							printf("MQTT connect error - ret : %d\r\n", ret);
						}
#endif
						atci.mqtt_con = 0;
						atci.mqtt_run = 1;
					}
				}
				break;
			default:
				ctlsocket(i, CS_GET_INTERRUPT, (uint8_t*)&ret);
				if((uint8_t)ret & Sn_IR_TIMEOUT){
					//ctlsocket(i, CS_CLR_INTERRUPT, (uint8_t*)&ret);
					close(i);
					WCF_HANDLE(WATCH_SOCK_CONN_TRY, SOCKERR_TIMEOUT);
				}
				break;
			}
		}
		if(watch_sock[i] & WATCH_SOCK_TCP_SEND) {
			// 블로킹 모드로만 동작함 그러므로 Watch할 필요가 없음
		}
		if(watch_sock[i] & WATCH_SOCK_UDP_SEND) {
			if(checkAtcUdpSendStatus < 0) {
				WCF_HANDLE(WATCH_SOCK_UDP_SEND, RET_NOK);
			}
			else {
				WCF_HANDLE(WATCH_SOCK_UDP_SEND, RET_OK);
			}
		}
	}

	// ToDo: not socket part
	
}
void set_mac(uint8_t *mac)
{
	S2E_Packet *value = get_S2E_Packet_pointer();

	memcpy(value->network_info_common.mac, mac, sizeof(value->network_info_common.mac));
}