3      Copyright (C) Dean Camera, 2014. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2014  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 
  31 #if defined(TEMPLATE_FUNC_NAME) 
  33 uint8_t TEMPLATE_FUNC_NAME (const void* const Buffer
, 
  36         uint8_t* DataStream     
= ((uint8_t*)Buffer 
+ TEMPLATE_BUFFER_OFFSET(Length
)); 
  37         bool     LastPacketFull 
= false; 
  39         Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint 
| ENDPOINT_DIR_IN
); 
  41         if (Length 
> USB_ControlRequest
.wLength
) 
  42           Length 
= USB_ControlRequest
.wLength
; 
  46         while (Length 
|| LastPacketFull
) 
  48                 uint8_t USB_DeviceState_LCL 
= USB_DeviceState
; 
  50                 if (USB_DeviceState_LCL 
== DEVICE_STATE_Unattached
) 
  51                   return ENDPOINT_RWCSTREAM_DeviceDisconnected
; 
  52                 else if (USB_DeviceState_LCL 
== DEVICE_STATE_Suspended
) 
  53                   return ENDPOINT_RWCSTREAM_BusSuspended
; 
  54                 else if (Endpoint_IsSETUPReceived()) 
  55                   return ENDPOINT_RWCSTREAM_HostAborted
; 
  56                 else if (Endpoint_IsOUTReceived()) 
  59                 if (Endpoint_IsINReady()) 
  61                         uint16_t BytesInEndpoint 
= Endpoint_BytesInEndpoint(); 
  63                         while (Length 
&& (BytesInEndpoint 
< USB_Device_ControlEndpointSize
)) 
  65                                 TEMPLATE_TRANSFER_BYTE(DataStream
); 
  66                                 TEMPLATE_BUFFER_MOVE(DataStream
, 1); 
  71                         LastPacketFull 
= (BytesInEndpoint 
== USB_Device_ControlEndpointSize
); 
  76         while (!(Endpoint_IsOUTReceived())) 
  78                 uint8_t USB_DeviceState_LCL 
= USB_DeviceState
; 
  80                 if (USB_DeviceState_LCL 
== DEVICE_STATE_Unattached
) 
  81                   return ENDPOINT_RWCSTREAM_DeviceDisconnected
; 
  82                 else if (USB_DeviceState_LCL 
== DEVICE_STATE_Suspended
) 
  83                   return ENDPOINT_RWCSTREAM_BusSuspended
; 
  84                 else if (Endpoint_IsSETUPReceived()) 
  85                   return ENDPOINT_RWCSTREAM_HostAborted
; 
  88         return ENDPOINT_RWCSTREAM_NoError
; 
  91 #undef TEMPLATE_BUFFER_OFFSET 
  92 #undef TEMPLATE_BUFFER_MOVE 
  93 #undef TEMPLATE_FUNC_NAME 
  94 #undef TEMPLATE_TRANSFER_BYTE