/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();

	TCP_Init();
	Webserver_Init();

	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	sei();

	for (;;)
	{
		if (RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface))
		{
			LEDs_SetAllLEDs(LEDMASK_USB_BUSY);

			RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface, &FrameIN.FrameData, &FrameIN.FrameLength);
			Ethernet_ProcessPacket(&FrameIN, &FrameOUT);

			if (FrameOUT.FrameLength)
			{
				RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface, &FrameOUT.FrameData, FrameOUT.FrameLength);
				FrameOUT.FrameLength = 0;
			}

			LEDs_SetAllLEDs(LEDMASK_USB_READY);
		}

		TCP_TCPTask(&Ethernet_RNDIS_Interface, &FrameOUT);

		RNDIS_Device_USBTask(&Ethernet_RNDIS_Interface);
		USB_USBTask();
	}
}
예제 #2
0
/** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */
static void uIPManagement_ProcessIncomingPacket(void)
{
	/* Determine which USB mode the system is currently initialized in */
	if (USB_CurrentMode == USB_MODE_Device)
	{
		/* If no packet received, exit processing routine */
		if (!(RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface_Device)))
		  return;

		LEDs_SetAllLEDs(LEDMASK_USB_BUSY);

		/* Read the Incoming packet straight into the UIP packet buffer */
		RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, &uip_len);
	}
	else
	{
		/* If no packet received, exit processing routine */
		if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface_Host)))
		  return;

		LEDs_SetAllLEDs(LEDMASK_USB_BUSY);

		/* Read the Incoming packet straight into the UIP packet buffer */
		RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, &uip_len);
	}

	/* If the packet contains an Ethernet frame, process it */
	// cppcheck-suppress redundantOperationIn
	if (uip_len > 0)
	{
		switch (((struct uip_eth_hdr*)uip_buf)->type)
		{
			case HTONS(UIP_ETHTYPE_IP):
				/* Filter packet by MAC destination */
				uip_arp_ipin();

				/* Process Incoming packet */
				uip_input();

				/* If a response was generated, send it */
				if (uip_len > 0)
				{
					/* Add destination MAC to outgoing packet */
					uip_arp_out();

					uip_split_output();
				}

				break;
			case HTONS(UIP_ETHTYPE_ARP):
				/* Process ARP packet */
				uip_arp_arpin();

				/* If a response was generated, send it */
				if (uip_len > 0)
				  uip_split_output();

				break;
		}
	}

	LEDs_SetAllLEDs(LEDMASK_USB_READY);
}