/** * Gets the configuration descriptor of a USB device as a byte array. * @param device USB device * @param conf configuration number * @param length length of the data buffer. This method will not write beyond this boundary. * @return number of bytes read, or negative number in case of error. */ int usb_getConfigurationDescriptor(usb_device * device, uint8_t conf, uint16_t length, uint8_t * data) { uint16_t descriptorLength; int rcode; // Read the length of the configuration descriptor. rcode = (usb_controlRequest(device, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, 4, data)); if (rcode) return -1; descriptorLength = (data[3] << 8) | data[2]; if (descriptorLength<length) length = descriptorLength; // Read the length of the configuration descriptor. rcode = (usb_controlRequest(device, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, length, data)); if (rcode) return -2; return length; }
/** * Convenience method for getting a string. This is useful for printing manufacturer names, serial numbers, etc. * Language is defaulted to zero. Strings are returned in 16-bit unicode from the device, so this function converts the * result to ASCII by ignoring every second byte. However, since the target buffer is used as a temporary storage during * this process it must be twice as large as the desired maximum string size. * * @param device USB device. * @param index string index. * @param languageId language ID. * @param length buffer length. * @param str target buffer. * @return 0 on success, error code otherwise. */ int usb_getString(usb_device * device, uint8_t index, uint8_t languageId, uint16_t length, char * str) { uint8_t stringLength = 0; int i, ret = 0; // Get string length; ret = usb_controlRequest(device, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, languageId, sizeof(uint8_t), &stringLength); if (ret<0) return -1; // Trim string size to fit the target buffer. if (stringLength>length) stringLength = length; // Get the whole thing. ret = usb_controlRequest(device, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, languageId, stringLength, (uint8_t *)str); if (ret<0) return -2; // Convert to 8-bit ASCII stringLength = (stringLength - 2) / 2; for (i=0; i<stringLength; i++) str[i] = str[2+i*2]; str[stringLength] = 0; return 0; }
//set configuration int usb_setConfiguration(usb_device * device, uint8_t configuration) { return(usb_controlRequest(device, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, configuration, 0x00, 0x0000, 0x0000, NULL)); }
/** * Sets the address of a newly connected USB device. * * @param device the 'zero' usb device (address 0, endpoint 0) * @param address the address to set for the newly connected device * @return 0 in case of success, error code otherwise */ int usb_setAddress(usb_device * device, uint8_t address) { return(usb_controlRequest(device, bmREQ_SET, USB_REQUEST_SET_ADDRESS, address, 0x00, 0x0000, 0x0000, NULL)); }
/** * Gets the device descriptor of a USB device. * @param device USB device * @param descriptor pointer to a usb_deviceDescriptor record that will be filled with the requested data. * @return 0 in case of success, error code otherwise */ int usb_getDeviceDescriptor(usb_device * device, usb_deviceDescriptor * descriptor) { return(usb_controlRequest(device, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, sizeof(usb_deviceDescriptor), (uint8_t *)descriptor)); }
/** * Sets the HID protocol not to report until an event occurs i.e set to idle. */ static void hid_setIdle(usb_device * device) { usb_controlRequest(device, bmREQ_HIDOUT, HID_REQUEST_SET_IDLE, 0x00, 0x00, 0x0000, 0x0000, NULL); }