bool AndroidAccessory::isConnected(void) { USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) descBuff; byte err; refresh(); if (!connected && usb.getUsbTaskState() >= USB_STATE_CONFIGURING && usb.getUsbTaskState() != USB_STATE_RUNNING) { Serial.print(F("\nDevice addressed... ")); Serial.print(F("Requesting device descriptor.\n")); err = usb.getDevDescr(1, 0, 0x12, (char *) devDesc); if (err) { Serial.print(F("\nDevice descriptor cannot be retrieved. Trying again\n")); return false; } if (isAccessoryDevice(devDesc)) { Serial.print(F("found android acessory device\n")); connected = configureAndroid(); } else { Serial.print(F("found possible device. swithcing to serial mode\n")); switchDevice(1); } } else if (usb.getUsbTaskState() == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) { if (connected) Serial.println(F("disconnect\n")); connected = false; } return connected; }
bool AndroidAccessory::connect() { USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) descBuff; byte err = usb.getDevDescr(1, 0, 0x12, (char *) devDesc); if (err) { return false; } if (isAccessoryDevice(devDesc)) { return configureAndroid(); } else { switchDevice(1); } return false; }
//***************************************************************************** // //! This function is used to open an instance of the ANDROID driver and configure it as Open Accessory. //! //! \param pDevice is a pointer to the device information structure. //! //! This function will attempt to open an instance of the ANDROID driver based on //! the information contained in the pDevice structure. This call can fail if //! there are not sufficient resources to open the device. The function will //! return a value that should be passed back into USBANDROIDClose() when the //! driver is no longer needed. //! //! \return The function will return a pointer to a ANDROID driver instance. // //***************************************************************************** static void * USBHANDROIDOpen(tUSBHostDevice *pDevice) { int iIdx; tEndpointDescriptor *pEndpointDescriptor; tInterfaceDescriptor *pInterface; UARTprintf("\nStart USBHANDROIDOpen Time=%d\n", g_ulSysTickCount); /* For Debug purpose you can also list full Configuration Descriptor & Device Descriptor */ getConfigDesc(pDevice); getDeviceDesc(pDevice); /* UARTprintf("Start pDevice->pConfigDescriptor details Time=%d:\n", g_ulSysTickCount); UARTprintf(" bLength=0x%02X (shall be 0x09)\n", pDevice->pConfigDescriptor->bLength); UARTprintf(" wTotalLength=0x%04X\n", pDevice->pConfigDescriptor->wTotalLength); UARTprintf(" bDescriptorType=0x%02X\n", pDevice->pConfigDescriptor->bDescriptorType); UARTprintf(" bNumInterfaces=0x%02X\n", pDevice->pConfigDescriptor->bNumInterfaces); UARTprintf(" bConfigurationValue=0x%02X\n", pDevice->pConfigDescriptor->bConfigurationValue); UARTprintf(" iConfiguration=0x%02X\n", pDevice->pConfigDescriptor->iConfiguration); UARTprintf(" bmAttributes=0x%02X\n", pDevice->pConfigDescriptor->bmAttributes); UARTprintf(" bMaxPower=0x%02X (unit of 2mA)\n\n", pDevice->pConfigDescriptor->bMaxPower); UARTprintf("pDevice->DeviceDescriptor details:\n"); UARTprintf(" bLength=0x%02X (shall be 0x12)\n", pDevice->DeviceDescriptor.bLength); UARTprintf(" bDescriptorType=0x%02X\n", pDevice->DeviceDescriptor.bDescriptorType); UARTprintf(" bcdUSB=0x%02X (USB2.0=0x200)\n", pDevice->DeviceDescriptor.bcdUSB); UARTprintf(" bDeviceClass=0x%02X\n", pDevice->DeviceDescriptor.bDeviceClass); UARTprintf(" bDeviceSubClass=0x%02X\n", pDevice->DeviceDescriptor.bDeviceSubClass); UARTprintf(" bDeviceProtocol=0x%02X\n", pDevice->DeviceDescriptor.bDeviceProtocol); UARTprintf(" bMaxPacketSize0=0x%02X\n", pDevice->DeviceDescriptor.bMaxPacketSize0); UARTprintf(" idVendor=0x%02X\n", pDevice->DeviceDescriptor.idVendor); UARTprintf(" idProduct=0x%02X\n", pDevice->DeviceDescriptor.idProduct); UARTprintf(" bcdDevice=0x%02X\n", pDevice->DeviceDescriptor.bcdDevice); UARTprintf(" iManufacturer=0x%02X\n", pDevice->DeviceDescriptor.iManufacturer); UARTprintf(" iProduct=0x%02X\n", pDevice->DeviceDescriptor.iProduct); UARTprintf(" iSerialNumber=0x%02X\n", pDevice->DeviceDescriptor.iSerialNumber); UARTprintf(" bNumConfigurations=0x%02X\n", pDevice->DeviceDescriptor.bNumConfigurations); UARTprintf("End pDevice->pConfigDescriptor details Time=%d:\n\n", g_ulSysTickCount); */ // Don't allow the device to be opened without closing first. if(g_USBHANDROIDDevice.pDevice) { UARTprintf("\nUSBHANDROIDOpen return 0 device already opened\n"); return(0); } // Save the device pointer. g_USBHANDROIDDevice.pDevice = pDevice; // Save the callback. // The CallBack is the driver callback for any Android ADK events. // // This function is called to open an instance of an android device. It // should be called before any devices are connected to allow for proper // notification of android connection and disconnection. The // application should also provide the \e pfnCallback to be notified of Andoid // ADK related events like device enumeration and device removal. g_USBHANDROIDDevice.pfnCallback = USBHANDROIDCallback; /* Check Android Accessory device */ if (isAccessoryDevice(&pDevice->DeviceDescriptor)) { UARTprintf("Found Android Accessory device Time=%d\n", g_ulSysTickCount); // Get the interface descriptor. pInterface = USBDescGetInterface(pDevice->pConfigDescriptor, 0, 0); // Loop through the endpoints of the device. for(iIdx = 0; iIdx < 3; iIdx++) { // Get the first endpoint descriptor. pEndpointDescriptor = USBDescGetInterfaceEndpoint(pInterface, iIdx, pDevice->ulConfigDescriptorSize); UARTprintf("\nEndpoint%d USBDescGetInterfaceEndpoint()=pEndpointDescriptor res=0x%08X\n", iIdx, pEndpointDescriptor); // If no more endpoints then break out. if(pEndpointDescriptor == 0) { break; } UARTprintf("pEndpointDescriptor details:\n"); UARTprintf(" bLength=0x%02X (shall be 0x07)\n", pEndpointDescriptor->bLength); UARTprintf(" bDescriptorType=0x%02X\n", pEndpointDescriptor->bDescriptorType); UARTprintf(" bEndpointAddress=0x%02X\n", pEndpointDescriptor->bEndpointAddress); UARTprintf(" bmAttributes=0x%02X (0x00=CTRL, 0x01=ISOC, 0x02=BULK, 0x03=INT\n", pEndpointDescriptor->bmAttributes); UARTprintf(" wMaxPacketSize=0x%04X\n", pEndpointDescriptor->wMaxPacketSize); UARTprintf(" bInterval=0x%04X\n", pEndpointDescriptor->bInterval); // See if this is a bulk endpoint. if((pEndpointDescriptor->bmAttributes & USB_EP_ATTR_TYPE_M) == USB_EP_ATTR_BULK) { // See if this is bulk IN or bulk OUT. if(pEndpointDescriptor->bEndpointAddress & USB_EP_DESC_IN) { UARTprintf("Endpoint Bulk In alloc USB Pipe\n"); // Allocate the USB Pipe for this Bulk IN endpoint. g_USBHANDROIDDevice.ulBulkInPipe = USBHCDPipeAllocSize(0, USBHCD_PIPE_BULK_IN_DMA, pDevice->ulAddress, pEndpointDescriptor->wMaxPacketSize, 0); // Configure the USB pipe as a Bulk IN endpoint. USBHCDPipeConfig(g_USBHANDROIDDevice.ulBulkInPipe, pEndpointDescriptor->wMaxPacketSize, BULK_READ_TIMEOUT, (pEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M)); } else { UARTprintf("Endpoint Bulk OUT alloc USB Pipe\n"); // Allocate the USB Pipe for this Bulk OUT endpoint. g_USBHANDROIDDevice.ulBulkOutPipe = USBHCDPipeAllocSize(0, USBHCD_PIPE_BULK_OUT_DMA, pDevice->ulAddress, pEndpointDescriptor->wMaxPacketSize, 0); // Configure the USB pipe as a Bulk OUT endpoint. USBHCDPipeConfig(g_USBHANDROIDDevice.ulBulkOutPipe, pEndpointDescriptor->wMaxPacketSize, BULK_WRITE_TIMEOUT, (pEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M)); } } } // If the callback exists, call it with an Open event. if(g_USBHANDROIDDevice.pfnCallback != 0) { g_USBHANDROIDDevice.pfnCallback((t_u32)&g_USBHANDROIDDevice, ANDROID_EVENT_OPEN, 0); } // Set Flag isConnected g_USBHANDROIDDevice.connected = true; } else { UARTprintf("Found possible device. switching to serial mode Time=%d\n", g_ulSysTickCount); switchDevice(pDevice); // Set Flag isConnected g_USBHANDROIDDevice.connected = false; } UARTprintf("\nEnd USBHANDROIDOpen Time=%d\n", g_ulSysTickCount); // Return the only instance of this device. return(&g_USBHANDROIDDevice); }