/*
             LUFA Library
     Copyright (C) Dean Camera, 2012.
              
  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this 
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in 
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting 
  documentation, and that the name of the author not be used in 
  advertising or publicity pertaining to distribution of the 
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/
 
 /*
 * CPPFile1.cpp
 *
 * Created: 14. 10. 2015 21:26:28
 *  Author: Lukas Patocka
 */ 
 
#include "Descriptors.h"
#include "DPv4.h"

const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
	.Header = 
	{
		.Size = sizeof(USB_Descriptor_Device_t),
		.Type = DTYPE_Device
	},
	.USBSpecification		= VERSION_BCD(1, 1, 0),
	// class of device
	.Class					= USB_CSCP_NoDeviceClass,
	// subclas of device
	.SubClass				= USB_CSCP_NoDeviceSubclass,
	// device protocol
	.Protocol				= USB_CSCP_NoDeviceProtocol,
	// size of control endpoint
	.Endpoint0Size			= FIXED_CONTROL_ENDPOINT_SIZE,

	// VID
	.VendorID				= 0x03EB,
	// PID
	.ProductID				= 0x2066,	
	// release number
	.ReleaseNumber			= VERSION_BCD(0, 0, 1),
	
	// index of string descriptors
	.ManufacturerStrIndex	= 1,
	.ProductStrIndex		= 2,
	// serial number- use default of controller
	.SerialNumStrIndex		= USE_INTERNAL_SERIAL,
	// count of configurations
	.NumberOfConfigurations	= FIXED_NUM_CONFIGURATIONS
};


const USB_Descriptor_Configuration_t PROGMEM ConfDescriptor =
{
	.Configuration					=
	{
		.Header						= 
		{
			.Size					= sizeof(USB_Descriptor_Configuration_Header_t),
			.Type					= DTYPE_Configuration
		},
		.TotalConfigurationSize		= sizeof(USB_Descriptor_Configuration_t),
		
		// total count of interfaces
		.TotalInterfaces			= 1,
		// total count of configurations
		.ConfigurationNumber		= 1,
		// string descriptor for configuration
		.ConfigurationStrIndex		= NO_DESCRIPTOR,
		// configuration parameters
		.ConfigAttributes			= (USB_CONFIG_ATTR_RESERVED),
		// supply power
		.MaxPowerConsumption		= USB_CONFIG_POWER_MA(100)
	},
	
	.Interface						=
	{
		.Header						=
		{
			.Size					= sizeof(USB_Descriptor_Interface_t),
			.Type					= DTYPE_Interface
		},
		
		// number of this interface
		.InterfaceNumber			= 0,
		.AlternateSetting			= 0,
		// total count of endpoints
		.TotalEndpoints				= 4,
		// class of device
		.Class						= USB_CSCP_NoDeviceClass,
		// subclass of device
		.SubClass					= USB_CSCP_NoDeviceSubclass,
		// device protocol
		.Protocol					= USB_CSCP_NoDeviceProtocol,
		// string descriptor for interface
		.InterfaceStrIndex			= NO_DESCRIPTOR
		
	},
	
	.IsoEpOut					=
	{
		.Header                 =
		{
			.Size				= sizeof(USB_Descriptor_Endpoint_t),
			.Type				= DTYPE_Endpoint
		},
		.EndpointAddress        = EP_ISO_OUT,
		.Attributes             = (EP_TYPE_ISOCHRONOUS | ENDPOINT_ATTR_ASYNC | ENDPOINT_USAGE_DATA),
		.EndpointSize           = EP_ISO_SIZE,
		.PollingIntervalMS      = 100
	},
	
	.IsoEpIn					=
	{
		.Header                 =
		{
			.Size				= sizeof(USB_Descriptor_Endpoint_t),
			.Type				= DTYPE_Endpoint
		},

		.EndpointAddress        = EP_ISO_IN,
		.Attributes             = (EP_TYPE_ISOCHRONOUS | ENDPOINT_ATTR_ASYNC | ENDPOINT_USAGE_DATA),
		.EndpointSize           = EP_ISO_SIZE,
		.PollingIntervalMS      = 100
	},

	.BulkEpOut					=
	{
		.Header					=
		{
			.Size				= sizeof(USB_Descriptor_Endpoint_t),
			.Type				= DTYPE_Endpoint
		},

		.EndpointAddress        = EP_BULK_OUT,
		.Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
		.EndpointSize           = EP_BULK_SIZE,
		.PollingIntervalMS      = 100
	},
	
	// bulk input endpoint definition
	.BulkEpIn					=
	{
		.Header					=
		{
			.Size				= sizeof(USB_Descriptor_Endpoint_t),
			.Type				= DTYPE_Endpoint
		},

		.EndpointAddress        = EP_BULK_IN,
		.Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
		.EndpointSize           = EP_BULK_SIZE,
		.PollingIntervalMS      = 100
	}
};


const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);

const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Lukas Patocka");

const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Xmega generator");


uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
									const uint8_t wIndex,	
									const void** const DescriptorAddress)
{
	const uint8_t  DescriptorType   = (wValue >> 8);
	const uint8_t  DescriptorNumber = (wValue & 0xFF);


	const void* Address = NULL;
	uint16_t    Size    = 0;

	switch (DescriptorType)
	{
		case DTYPE_Device:
			Address = &DeviceDescriptor;
			Size    = sizeof(USB_Descriptor_Device_t);		
			break;
		
		case DTYPE_Configuration:
			Address = &ConfDescriptor;
			Size    = sizeof(USB_Descriptor_Configuration_t);
			break;
		
		case DTYPE_String:
			switch (DescriptorNumber)
			{
				case 0x00:
				Address = &LanguageString;
				Size    = pgm_read_byte(&LanguageString.Header.Size);
				break;
			
				case 0x01:
				Address = &ManufacturerString;
				Size    = pgm_read_byte(&ManufacturerString.Header.Size);
				break;
			
				case 0x02:
				Address = &ProductString;
				Size    = pgm_read_byte(&ProductString.Header.Size);
				break;
			}
			break;
	}
	
	*DescriptorAddress = Address;
	return Size;
}


void EVENT_USB_Device_ConfigurationChanged(void)
{
	bool result = true;
	
	result &= Endpoint_ConfigureEndpoint(EP_ISO_OUT, EP_TYPE_ISOCHRONOUS, EP_ISO_SIZE, 2);
	result &= Endpoint_ConfigureEndpoint(EP_ISO_IN, EP_TYPE_ISOCHRONOUS, EP_ISO_SIZE, 2);
	result &= Endpoint_ConfigureEndpoint(EP_BULK_OUT, EP_TYPE_BULK, EP_BULK_SIZE, 2);
	result &= Endpoint_ConfigureEndpoint(EP_BULK_IN, EP_TYPE_BULK, EP_BULK_SIZE, 2);

	if(!result)	LedBlinkRed();
	else LedBlinkYellow();
}
