Пример #1
0
// This sets the IP configuration on-the-fly
void ethernet_set_configuration(IPAddress ipAddress, IPAddress netMask, IPAddress gateWay)
{
	if ((gs_net_if.flags & NETIF_FLAG_DHCP) != 0)
	{
		// stop DHCP if it was used before
		dhcp_stop(&gs_net_if);
	}

	struct ip_addr x_ip_addr, x_net_mask, x_gateway;
	x_ip_addr.addr = ipAddress.GetV4LittleEndian();
	x_net_mask.addr = netMask.GetV4LittleEndian();
	x_gateway.addr = gateWay.GetV4LittleEndian();

	if (x_ip_addr.addr == 0)
	{
		// start DHCP and request a dynamic IP address
		dhcp_start(&gs_net_if);
	}
	else
	{
		// use static IP address
		netif_set_ipaddr(&gs_net_if, &x_ip_addr);
		netif_set_netmask(&gs_net_if, &x_net_mask);
		netif_set_gw(&gs_net_if, &x_gateway);

		// don't forget to set it up again
		netif_set_up(&gs_net_if);
	}
}
Пример #2
0
void start_ethernet(IPAddress ipAddress, IPAddress netMask, IPAddress gateWay, netif_status_callback_fn status_cb)
{
	struct ip_addr x_ip_addr, x_net_mask, x_gateway;
	extern err_t ethernetif_init(struct netif *netif);

	x_ip_addr.addr = ipAddress.GetV4LittleEndian();

	if (x_ip_addr.addr == 0)
	{
		x_net_mask.addr = 0;
		x_gateway.addr = 0;
	}
	else
	{
		x_net_mask.addr = netMask.GetV4LittleEndian();
		x_gateway.addr = gateWay.GetV4LittleEndian();
	}

	/* Add data to netif */
	netif_add(&gs_net_if, &x_ip_addr, &x_net_mask, &x_gateway, NULL, ethernetif_init, ethernet_input);

	/* Make it the default interface */
	netif_set_default(&gs_net_if);

	/* Setup callback function for netif status change */
	netif_set_status_callback(&gs_net_if, status_cb);

	/* Bring it up */
	if (x_ip_addr.addr == 0)
	{
		/* DHCP mode */
		dhcp_start(&gs_net_if);
	}
	else
	{
		/* Static mode */
		netif_set_up(&gs_net_if);
	}
}