diff options
Diffstat (limited to 'Demos/Host/LowLevel/AndroidAccessoryHost')
13 files changed, 3491 insertions, 0 deletions
diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.c b/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.c new file mode 100644 index 0000000000..63d94f78ca --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.c @@ -0,0 +1,263 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 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 disclaims 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. +*/ + +/** \file + * + * Main source file for the AndroidAccessoryHost demo. This file contains the main tasks + * of the demo and is responsible for the initial application hardware configuration. + */ + +#include "AndroidAccessoryHost.h" + +/** Main program entry point. This routine configures the hardware required by the application, then + * enters a loop to run the application tasks in sequence. + */ +int main(void) +{ + SetupHardware(); + + puts_P(PSTR(ESC_FG_CYAN "Android Accessory Host Demo running.\r\n" ESC_FG_WHITE)); + + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + GlobalInterruptEnable(); + + for (;;) + { + AndroidHost_Task(); + USB_USBTask(); + } +} + +/** Configures the board hardware and chip peripherals for the demo's functionality. */ +void SetupHardware(void) +{ +#if (ARCH == ARCH_AVR8) + /* Disable watchdog if enabled by bootloader/fuses */ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + /* Disable clock division */ + clock_prescale_set(clock_div_1); +#endif + + /* Hardware Initialization */ + Serial_Init(9600, false); + LEDs_Init(); + USB_Init(); + + /* Create a stdio stream for the serial port for stdin and stdout */ + Serial_CreateStream(NULL); +} + +/** Task to set the configuration of the attached device after it has been enumerated. */ +void AndroidHost_Task(void) +{ + if (USB_HostState != HOST_STATE_Configured) + return; + + /* Select the data IN pipe */ + Pipe_SelectPipe(ANDROID_DATA_IN_PIPE); + Pipe_Unfreeze(); + + /* Check to see if a packet has been received */ + if (Pipe_IsINReceived()) + { + /* Re-freeze IN pipe after the packet has been received */ + Pipe_Freeze(); + + /* Check if data is in the pipe */ + if (Pipe_IsReadWriteAllowed()) + { + uint8_t NextReceivedByte = Pipe_Read_8(); + uint8_t LEDMask = LEDS_NO_LEDS; + + if (NextReceivedByte & 0x01) + LEDMask |= LEDS_LED1; + + if (NextReceivedByte & 0x02) + LEDMask |= LEDS_LED2; + + if (NextReceivedByte & 0x04) + LEDMask |= LEDS_LED3; + + if (NextReceivedByte & 0x08) + LEDMask |= LEDS_LED4; + + LEDs_SetAllLEDs(LEDMask); + } + else + { + /* Clear the pipe after all data in the packet has been read, ready for the next packet */ + Pipe_ClearIN(); + } + } + + /* Re-freeze IN pipe after use */ + Pipe_Freeze(); +} + +/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and + * starts the library USB task to begin the enumeration and USB management process. + */ +void EVENT_USB_Host_DeviceAttached(void) +{ + puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE)); + LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); +} + +/** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and + * stops the library USB task management process. + */ +void EVENT_USB_Host_DeviceUnattached(void) +{ + puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE)); + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); +} + +/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully + * enumerated by the host and is now ready to be used by the application. + */ +void EVENT_USB_Host_DeviceEnumerationComplete(void) +{ + puts_P(PSTR("Getting Device Data.\r\n")); + + /* Get and process the configuration descriptor data */ + uint8_t ErrorCode = ProcessDeviceDescriptor(); + + bool RequiresModeSwitch = (ErrorCode == NonAccessoryModeAndroidDevice); + + /* Error out if the device is not an Android device or an error occurred */ + if ((ErrorCode != AccessoryModeAndroidDevice) && (ErrorCode != NonAccessoryModeAndroidDevice)) + { + if (ErrorCode == ControlError) + puts_P(PSTR(ESC_FG_RED "Control Error (Get Device).\r\n")); + else + puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n")); + + printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + return; + } + + printf_P(PSTR("Android Device Detected - %sAccessory mode.\r\n"), (RequiresModeSwitch ? "Non-" : "")); + + /* Check if a valid Android device was attached, but it is not current in Accessory mode */ + if (RequiresModeSwitch) + { + uint16_t AndroidProtocol; + + /* Fetch the version of the Android Accessory Protocol supported by the device */ + if ((ErrorCode = Android_GetAccessoryProtocol(&AndroidProtocol)) != HOST_SENDCONTROL_Successful) + { + printf_P(PSTR(ESC_FG_RED "Control Error (Get Protocol).\r\n" + " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + return; + } + + /* Validate the returned protocol version */ + if (AndroidProtocol != AOA_PROTOCOL_AccessoryV1) + { + puts_P(PSTR(ESC_FG_RED "Accessory Mode Not Supported.")); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + return; + } + + /* Send the device strings and start the Android Accessory Mode */ + Android_SendString(AOA_STRING_Manufacturer, "Dean Camera"); + Android_SendString(AOA_STRING_Model, "LUFA Android Demo"); + Android_SendString(AOA_STRING_Description, "LUFA Android Demo"); + Android_SendString(AOA_STRING_Version, "1.0"); + Android_SendString(AOA_STRING_URI, "http://www.lufa-lib.org"); + Android_SendString(AOA_STRING_Serial, "0000000012345678"); + + Android_StartAccessoryMode(); + return; + } + + puts_P(PSTR("Getting Config Data.\r\n")); + + /* Get and process the configuration descriptor data */ + if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead) + { + if (ErrorCode == ControlError) + puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n")); + else + puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n")); + + printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + return; + } + + /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */ + if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful) + { + printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n" + " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + return; + } + + puts_P(PSTR("Accessory Mode Android Enumerated.\r\n")); + LEDs_SetAllLEDs(LEDMASK_USB_READY); +} + +/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ +void EVENT_USB_Host_HostError(const uint8_t ErrorCode) +{ + USB_Disable(); + + printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n" + " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + for(;;); +} + +/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while + * enumerating an attached USB device. + */ +void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, + const uint8_t SubErrorCode) +{ + printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n" + " -- Error Code %d\r\n" + " -- Sub Error Code %d\r\n" + " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); +} + diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.h b/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.h new file mode 100644 index 0000000000..36b2bf91e1 --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.h @@ -0,0 +1,86 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 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 disclaims 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. +*/ + +/** \file + * + * Header file for AndroidAccessoryHost.c. + */ + +#ifndef _ANDROID_ACCESSORY_HOST_H_ +#define _ANDROID_ACCESSORY_HOST_H_ + + /* Includes: */ + #include <avr/io.h> + #include <avr/wdt.h> + #include <avr/pgmspace.h> + #include <avr/power.h> + #include <avr/interrupt.h> + #include <stdio.h> + + #include "DeviceDescriptor.h" + #include "ConfigDescriptor.h" + #include "Lib/AndroidAccessoryCommands.h" + + #include <LUFA/Drivers/Misc/TerminalCodes.h> + #include <LUFA/Drivers/USB/USB.h> + #include <LUFA/Drivers/Peripheral/Serial.h> + #include <LUFA/Drivers/Board/LEDs.h> + #include <LUFA/Platform/Platform.h> + + /* Macros: */ + /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */ + #define LEDMASK_USB_NOTREADY LEDS_LED1 + + /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */ + #define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3) + + /** LED mask for the library LED driver, to indicate that the USB interface is ready. */ + #define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4) + + /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */ + #define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3) + + /** LED mask for the library LED driver, to indicate that the USB interface is busy. */ + #define LEDMASK_USB_BUSY LEDS_LED2 + + /* Function Prototypes: */ + void SetupHardware(void); + void AndroidHost_Task(void); + + /* Event Handlers: */ + void EVENT_USB_Host_DeviceAttached(void); + void EVENT_USB_Host_DeviceUnattached(void); + void EVENT_USB_Host_DeviceEnumerationComplete(void); + void EVENT_USB_Host_HostError(const uint8_t ErrorCode); + void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, + const uint8_t SubErrorCode); + +#endif + diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.txt b/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.txt new file mode 100644 index 0000000000..70e0935c9f --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/AndroidAccessoryHost.txt @@ -0,0 +1,62 @@ +/** \file + * + * This file contains special DoxyGen information for the generation of the main page and other special + * documentation pages. It is not a project source file. + */ + +/** \mainpage Android Accessory Host Demo + * + * \section Sec_Compat Demo Compatibility: + * + * The following list indicates what microcontrollers are compatible with this demo. + * + * \li Series 7 USB AVRs (AT90USBxxx7) + * + * \section Sec_Info USB Information: + * + * The following table gives a rundown of the USB utilization of this demo. + * + * <table> + * <tr> + * <td><b>USB Mode:</b></td> + * <td>Host</td> + * </tr> + * <tr> + * <td><b>USB Class:</b></td> + * <td>Android Accessory Host Class</td> + * </tr> + * <tr> + * <td><b>USB Subclass:</b></td> + * <td>N/A</td> + * </tr> + * <tr> + * <td><b>Relevant Standards:</b></td> + * <td>Android Accessory Host Specification</td> + * </tr> + * <tr> + * <td><b>Supported USB Speeds:</b></td> + * <td>Full Speed Mode</td> + * </tr> + * </table> + * + * \section Sec_Description Project Description: + * + * Android Accessory Host demonstration application. This gives a simple reference + * application for implementing an Android Accessory Host device capable of hosting + * Android powered mobile devices to send and receive data. + * + * Sent data from the Android device will be indicated onto the board's LEDs. + * + * \section Sec_Options Project Options + * + * The following defines can be found in this demo, which can control the demo behaviour when defined, or changed in value. + * + * <table> + * <tr> + * <td> + * None + * </td> + * </tr> + * </table> + */ + diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/Config/LUFAConfig.h b/Demos/Host/LowLevel/AndroidAccessoryHost/Config/LUFAConfig.h new file mode 100644 index 0000000000..1d840f618c --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/Config/LUFAConfig.h @@ -0,0 +1,93 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 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 disclaims 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. +*/ + +/** \file + * \brief LUFA Library Configuration Header File + * + * This header file is used to configure LUFA's compile time options, + * as an alternative to the compile time constants supplied through + * a makefile. + * + * For information on what each token does, refer to the LUFA + * manual section "Summary of Compile Tokens". + */ + +#ifndef _LUFA_CONFIG_H_ +#define _LUFA_CONFIG_H_ + + #if (ARCH == ARCH_AVR8) + + /* Non-USB Related Configuration Tokens: */ +// #define DISABLE_TERMINAL_CODES + + /* USB Class Driver Related Tokens: */ +// #define HID_HOST_BOOT_PROTOCOL_ONLY +// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here} +// #define HID_USAGE_STACK_DEPTH {Insert Value Here} +// #define HID_MAX_COLLECTIONS {Insert Value Here} +// #define HID_MAX_REPORTITEMS {Insert Value Here} +// #define HID_MAX_REPORT_IDS {Insert Value Here} +// #define NO_CLASS_DRIVER_AUTOFLUSH + + /* General USB Driver Related Tokens: */ +// #define ORDERED_EP_CONFIG + #define USE_STATIC_OPTIONS (USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) +// #define USB_DEVICE_ONLY + #define USB_HOST_ONLY +// #define USB_STREAM_TIMEOUT_MS {Insert Value Here} +// #define NO_LIMITED_CONTROLLER_CONNECT +// #define NO_SOF_EVENTS + + /* USB Device Mode Driver Related Tokens: */ +// #define USE_RAM_DESCRIPTORS +// #define USE_FLASH_DESCRIPTORS +// #define USE_EEPROM_DESCRIPTORS +// #define NO_INTERNAL_SERIAL +// #define FIXED_CONTROL_ENDPOINT_SIZE {Insert Value Here} +// #define DEVICE_STATE_AS_GPIOR {Insert Value Here} +// #define FIXED_NUM_CONFIGURATIONS {Insert Value Here} +// #define CONTROL_ONLY_DEVICE +// #define INTERRUPT_CONTROL_ENDPOINT +// #define NO_DEVICE_REMOTE_WAKEUP +// #define NO_DEVICE_SELF_POWER + + /* USB Host Mode Driver Related Tokens: */ +// #define HOST_STATE_AS_GPIOR {Insert Value Here} +// #define USB_HOST_TIMEOUT_MS {Insert Value Here} + #define HOST_DEVICE_SETTLE_DELAY_MS 0 +// #define NO_AUTO_VBUS_MANAGEMENT +// #define INVERTED_VBUS_ENABLE_LINE + + #else + + #error Unsupported architecture for this LUFA configuration file. + + #endif +#endif diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/ConfigDescriptor.c b/Demos/Host/LowLevel/AndroidAccessoryHost/ConfigDescriptor.c new file mode 100644 index 0000000000..d4aa877620 --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/ConfigDescriptor.c @@ -0,0 +1,164 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 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 disclaims 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. +*/ + +/** \file + * + * USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations + * needed to communication with an attached USB device. Descriptors are special computer-readable structures + * which the host requests upon device enumeration, to determine the device's capabilities and functions. + */ + +#include "ConfigDescriptor.h" + +/** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This + * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate + * with compatible devices. + * + * This routine searches for the first interface containing bulk IN and OUT data endpoints. + * + * \return An error code from the \ref AndroidHost_GetConfigDescriptorDataCodes_t enum. + */ +uint8_t ProcessConfigurationDescriptor(void) +{ + uint8_t ConfigDescriptorData[512]; + void* CurrConfigLocation = ConfigDescriptorData; + uint16_t CurrConfigBytesRem; + + USB_Descriptor_Endpoint_t* DataINEndpoint = NULL; + USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL; + + /* Retrieve the entire configuration descriptor into the allocated buffer */ + switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData))) + { + case HOST_GETCONFIG_Successful: + break; + case HOST_GETCONFIG_InvalidData: + return InvalidConfigDataReturned; + case HOST_GETCONFIG_BuffOverflow: + return DescriptorTooLarge; + default: + return DevControlError; + } + + /* There should be only one compatible Android Accessory Mode interface in the device, attempt to find it */ + if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, + DCOMP_NextAndroidAccessoryInterface) != DESCRIPTOR_SEARCH_COMP_Found) + { + return NoCompatibleInterfaceFound; + } + + while (!(DataINEndpoint) || !(DataOUTEndpoint)) + { + /* Get the next Android Accessory Mode interface's data endpoint descriptor */ + if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, + DCOMP_NextInterfaceBulkEndpoint) != DESCRIPTOR_SEARCH_COMP_Found) + { + /* Data endpoints not found within the first Android Accessory device interface, error out */ + return NoCompatibleInterfaceFound; + } + + /* Retrieve the endpoint address from the endpoint descriptor */ + USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t); + + /* If the endpoint is a IN type endpoint */ + if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN) + DataINEndpoint = EndpointData; + else + DataOUTEndpoint = EndpointData; + } + + /* Configure the Android Accessory data IN pipe */ + Pipe_ConfigurePipe(ANDROID_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1); + + /* Configure the Android Accessory data OUT pipe */ + Pipe_ConfigurePipe(ANDROID_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1); + + /* Valid data found, return success */ + return SuccessfulConfigRead; +} + +/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's + * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration + * descriptor processing if an incompatible descriptor configuration is found. + * + * This comparator searches for the next Interface descriptor containing the correct Android Accessory Mode Class, Subclass + * and Protocol values. + * + * \return A value from the DSEARCH_Return_ErrorCodes_t enum + */ +uint8_t DCOMP_NextAndroidAccessoryInterface(void* const CurrentDescriptor) +{ + USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t); + + if (Header->Type == DTYPE_Interface) + { + USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t); + + if ((Interface->Class == AOA_CSCP_AOADataClass) && + (Interface->SubClass == AOA_CSCP_AOADataSubclass) && + (Interface->Protocol == AOA_CSCP_AOADataProtocol)) + { + return DESCRIPTOR_SEARCH_Found; + } + } + + return DESCRIPTOR_SEARCH_NotFound; +} + +/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's + * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration + * descriptor processing if an incompatible descriptor configuration is found. + * + * This comparator searches for the next bulk Endpoint descriptor inside the current interface descriptor, aborting the + * search if another interface descriptor is found before the required endpoint. + * + * \return A value from the DSEARCH_Return_ErrorCodes_t enum + */ +uint8_t DCOMP_NextInterfaceBulkEndpoint(void* const CurrentDescriptor) +{ + USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t); + + if (Header->Type == DTYPE_Endpoint) + { + USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t); + + uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK); + + if ((EndpointType == EP_TYPE_BULK) && (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))) + return DESCRIPTOR_SEARCH_Found; + } + else if (Header->Type == DTYPE_Interface) + { + return DESCRIPTOR_SEARCH_Fail; + } + + return DESCRIPTOR_SEARCH_NotFound; +} + diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/ConfigDescriptor.h b/Demos/Host/LowLevel/AndroidAccessoryHost/ConfigDescriptor.h new file mode 100644 index 0000000000..f422f8c2e2 --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/ConfigDescriptor.h @@ -0,0 +1,67 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 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 disclaims 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. +*/ + +/** \file + * + * Header file for ConfigDescriptor.c. + */ + +#ifndef _CONFIGDESCRIPTOR_H_ +#define _CONFIGDESCRIPTOR_H_ + + /* Includes: */ + #include <LUFA/Drivers/USB/USB.h> + + /* Macros: */ + /** Pipe address of the Android Accessory data IN pipe. */ + #define ANDROID_DATA_IN_PIPE (PIPE_DIR_IN | 1) + + /** Pipe address of the Android Accessory data OUT pipe. */ + #define ANDROID_DATA_OUT_PIPE (PIPE_DIR_OUT | 2) + + /* Enums: */ + /** Enum for the possible return codes of the \ref ProcessConfigurationDescriptor() function. */ + enum AndroidHost_GetConfigDescriptorDataCodes_t + { + SuccessfulConfigRead = 0, /**< Configuration Descriptor was processed successfully */ + DevControlError = 1, /**< A control request to the device failed to complete successfully */ + DescriptorTooLarge = 2, /**< The device's Configuration Descriptor is too large to process */ + InvalidConfigDataReturned = 3, /**< The device returned an invalid Configuration Descriptor */ + NoCompatibleInterfaceFound = 4, /**< A compatible interface with the required endpoints was not found */ + }; + + /* Function Prototypes: */ + uint8_t ProcessConfigurationDescriptor(void); + + uint8_t DCOMP_NextAndroidAccessoryInterface(void* const CurrentDescriptor); + uint8_t DCOMP_NextInterfaceBulkEndpoint(void* CurrentDescriptor); + +#endif + diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/DeviceDescriptor.c b/Demos/Host/LowLevel/AndroidAccessoryHost/DeviceDescriptor.c new file mode 100644 index 0000000000..03841a9903 --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/DeviceDescriptor.c @@ -0,0 +1,67 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 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 disclaims 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. +*/ + +/** \file + * + * USB Device Descriptor processing routines, to determine the overall device parameters. Descriptors are special + * computer-readable structures which the host requests upon device enumeration, to determine information about + * the attached device. + */ + +#include "DeviceDescriptor.h" + +/** Reads and processes an attached device's Device Descriptor, to determine compatibility + * + * This routine checks to ensure that the attached device's VID and PID matches Google's for Android devices. + * + * \return An error code from the \ref AndroidHost_GetDeviceDescriptorDataCodes_t enum. + */ +uint8_t ProcessDeviceDescriptor(void) +{ + USB_Descriptor_Device_t DeviceDescriptor; + + /* Send the request to retrieve the device descriptor */ + if (USB_Host_GetDeviceDescriptor(&DeviceDescriptor) != HOST_SENDCONTROL_Successful) + return DevControlError; + + /* Validate returned data - ensure the returned data is a device descriptor */ + if (DeviceDescriptor.Header.Type != DTYPE_Device) + return InvalidDeviceDataReturned; + + /* Check the product ID to determine if the Android device is in accessory mode */ + if ((DeviceDescriptor.ProductID != ANDROID_ACCESSORY_PRODUCT_ID) && + (DeviceDescriptor.ProductID != ANDROID_ACCESSORY_ADB_PRODUCT_ID)) + { + return NonAccessoryModeAndroidDevice; + } + + return AccessoryModeAndroidDevice; +} + diff --git a/Demos/Host/LowLevel/AndroidAccessoryHost/DeviceDescriptor.h b/Demos/Host/LowLevel/AndroidAccessoryHost/DeviceDescriptor.h new file mode 100644 index 0000000000..3324b2a551 --- /dev/null +++ b/Demos/Host/LowLevel/AndroidAccessoryHost/DeviceDescriptor.h @@ -0,0 +1,60 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 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 disclaims 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. +*/ + +/** \file + * + * Header file for DeviceDescriptor.c. + */ + +#ifndef _DEVICEDESCRIPTOR_H_ +#define _DEVICEDESCRIPTOR_H_ + + /* Includes: */ + #include <LUFA/Drivers/USB/USB.h> + + #include "AndroidAccessoryHost.h" + + /* Enums: */ + /** Enum for the possible return codes of the \ref ProcessDeviceDescriptor() function. */ + enum AndroidHost_GetDeviceDescriptorDataCodes_t + { + SuccessfulDeviceRead = 0, /**< Device Descriptor was processed successfully */ + ControlError = 1, /**< A control request to the device failed to complete successfully */ + InvalidDeviceDataReturned = 2, /**< The device returned an invalid Device Descriptor */ + IncorrectAndroidDevice = 3, /**< The attached device is not an Android device */ + NonAccessoryModeAndroidDevice = 4, /**< The attached device is an Android device in non-accessory mode */ + AccessoryModeAndroidDevice = 5, /**< The attached device is an Android device in accessory mode */ + }; + + /* Function Prototypes: */ |