3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaims all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Main source file for the RNDISEthernet demo. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  37 #include "RNDISEthernet.h" 
  39 /** Main program entry point. This routine configures the hardware required by the application, then 
  40  *  enters a loop to run the application tasks in sequence. 
  46         /* Webserver Initialization */ 
  50         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  51         GlobalInterruptEnable(); 
  62 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  63 void SetupHardware(void) 
  65         /* Disable watchdog if enabled by bootloader/fuses */ 
  66         MCUSR 
&= ~(1 << WDRF
); 
  69         /* Disable clock division */ 
  70         clock_prescale_set(clock_div_1
); 
  72         /* Hardware Initialization */ 
  74         Serial_Init(9600, false); 
  77         /* Create a stdio stream for the serial port for stdin and stdout */ 
  78         Serial_CreateStream(NULL
); 
  81 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
  82  *  starts the library USB task to begin the enumeration and USB management process. 
  84 void EVENT_USB_Device_Connect(void) 
  86         /* Indicate USB enumerating */ 
  87         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  90 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
  91  *  the status LEDs and stops all the relevant tasks. 
  93 void EVENT_USB_Device_Disconnect(void) 
  95         /* Indicate USB not ready */ 
  96         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  99 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration 
 100  *  of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks. 
 102 void EVENT_USB_Device_ConfigurationChanged(void) 
 104         bool ConfigSuccess 
= true; 
 106         /* Setup RNDIS Data Endpoints */ 
 107         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(CDC_TX_EPADDR
, EP_TYPE_BULK
, CDC_TXRX_EPSIZE
, 1); 
 108         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(CDC_RX_EPADDR
, EP_TYPE_BULK
, CDC_TXRX_EPSIZE
, 1); 
 109         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR
, EP_TYPE_INTERRUPT
, CDC_NOTIFICATION_EPSIZE
, 1); 
 111         /* Indicate endpoint configuration success or failure */ 
 112         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 115 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
 116  *  the device from the USB host before passing along unhandled control requests to the library for processing 
 119 void EVENT_USB_Device_ControlRequest(void) 
 121         /* Process RNDIS class commands */ 
 122         switch (USB_ControlRequest
.bRequest
) 
 124                 case RNDIS_REQ_SendEncapsulatedCommand
: 
 125                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 127                                 Endpoint_ClearSETUP(); 
 129                                 /* Read in the RNDIS message into the message buffer */ 
 130                                 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer
, USB_ControlRequest
.wLength
); 
 133                                 /* Process the RNDIS message */ 
 134                                 ProcessRNDISControlMessage(); 
 138                 case RNDIS_REQ_GetEncapsulatedResponse
: 
 139                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 141                                 /* Check if a response to the last message is ready */ 
 142                                 if (!(MessageHeader
->MessageLength
)) 
 144                                         /* Set the response to a single 0x00 byte to indicate that no response is ready */ 
 145                                         RNDISMessageBuffer
[0] = 0; 
 146                                         MessageHeader
->MessageLength 
= 1; 
 149                                 Endpoint_ClearSETUP(); 
 151                                 /* Write the message response data to the endpoint */ 
 152                                 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer
, MessageHeader
->MessageLength
); 
 155                                 /* Reset the message header once again after transmission */ 
 156                                 MessageHeader
->MessageLength 
= 0; 
 163 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS 
 164  *  wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper 
 165  *  to a frame in the FrameOUT global before sending the buffer contents to the host. 
 167 void RNDIS_Task(void) 
 169         /* Select the notification endpoint */ 
 170         Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPADDR
); 
 172         /* Check if a message response is ready for the host */ 
 173         if (Endpoint_IsINReady() && ResponseReady
) 
 175                 USB_Request_Header_t Notification 
= (USB_Request_Header_t
) 
 177                                 .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 178                                 .bRequest      
= RNDIS_NOTIF_ResponseAvailable
, 
 184                 /* Indicate that a message response is ready for the host */ 
 185                 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
), NULL
); 
 187                 /* Finalize the stream transfer to send the last packet */ 
 190                 /* Indicate a response is no longer ready */ 
 191                 ResponseReady 
= false; 
 194         /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */ 
 195         if ((CurrRNDISState 
== RNDIS_Data_Initialized
) && !(MessageHeader
->MessageLength
)) 
 197                 /* Create a new packet header for reading/writing */ 
 198                 RNDIS_Packet_Message_t RNDISPacketHeader
; 
 200                 /* Select the data OUT endpoint */ 
 201                 Endpoint_SelectEndpoint(CDC_RX_EPADDR
); 
 203                 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */ 
 204                 if (Endpoint_IsOUTReceived() && !(FrameIN
.FrameLength
)) 
 206                         /* Read in the packet message header */ 
 207                         Endpoint_Read_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_Packet_Message_t
), NULL
); 
 209                         /* Stall the request if the data is too large */ 
 210                         if (RNDISPacketHeader
.DataLength 
> ETHERNET_FRAME_SIZE_MAX
) 
 212                                 Endpoint_StallTransaction(); 
 216                         /* Read in the Ethernet frame into the buffer */ 
 217                         Endpoint_Read_Stream_LE(FrameIN
.FrameData
, RNDISPacketHeader
.DataLength
, NULL
); 
 219                         /* Finalize the stream transfer to send the last packet */ 
 222                         /* Store the size of the Ethernet frame */ 
 223                         FrameIN
.FrameLength 
= RNDISPacketHeader
.DataLength
; 
 226                 /* Select the data IN endpoint */ 
 227                 Endpoint_SelectEndpoint(CDC_TX_EPADDR
); 
 229                 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */ 
 230                 if (Endpoint_IsINReady() && FrameOUT
.FrameLength
) 
 232                         /* Clear the packet header with all 0s so that the relevant fields can be filled */ 
 233                         memset(&RNDISPacketHeader
, 0, sizeof(RNDIS_Packet_Message_t
)); 
 235                         /* Construct the required packet header fields in the buffer */ 
 236                         RNDISPacketHeader
.MessageType   
= REMOTE_NDIS_PACKET_MSG
; 
 237                         RNDISPacketHeader
.MessageLength 
= (sizeof(RNDIS_Packet_Message_t
) + FrameOUT
.FrameLength
); 
 238                         RNDISPacketHeader
.DataOffset    
= (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)); 
 239                         RNDISPacketHeader
.DataLength    
= FrameOUT
.FrameLength
; 
 241                         /* Send the packet header to the host */ 
 242                         Endpoint_Write_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_Packet_Message_t
), NULL
); 
 244                         /* Send the Ethernet frame data to the host */ 
 245                         Endpoint_Write_Stream_LE(FrameOUT
.FrameData
, RNDISPacketHeader
.DataLength
, NULL
); 
 247                         /* Finalize the stream transfer to send the last packet */ 
 250                         /* Indicate Ethernet OUT buffer no longer full */ 
 251                         FrameOUT
.FrameLength 
= 0; 
 256 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing 
 257  *  of the frame to the Ethernet processing routines. 
 259 void Ethernet_Task(void) 
 261         /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and 
 262            outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single 
 263            Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */ 
 265         /* Device must be connected and configured for the task to run */ 
 266         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 269         /* Check if a frame has been written to the IN frame buffer */ 
 270         if (FrameIN
.FrameLength
) 
 272                 /* Indicate packet processing started */ 
 273                 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 275                 /* Process the ethernet frame - replace this with your own Ethernet handler code as desired */ 
 276                 Ethernet_ProcessPacket(); 
 278                 /* Indicate packet processing complete */ 
 279                 LEDs_SetAllLEDs(LEDMASK_USB_READY
);