📄 controlmessage.java
字号:
byte id,
int index,
int len
) throws IOException
{
return getDescriptor (dev, TYPE_STANDARD,
descriptorType, id, index, len);
}
/**
* Requests a USB class descriptor from the specified device.
*/
public static byte [] getClassDescriptor (
Device dev,
byte descriptorType,
byte id,
int index,
int len
) throws IOException
{
return getDescriptor (dev, TYPE_CLASS,
descriptorType, id, index, len);
}
/**
* Requests a USB vendor descriptor from the specified device.
*/
public static byte [] getVendorDescriptor (
Device dev,
byte descriptorType,
byte id,
int index,
int len
) throws IOException
{
return getDescriptor (dev, TYPE_VENDOR,
descriptorType, id, index, len);
}
/**
* Returns a descriptor from the specified device.
*
* @param descriptorClass TYPE_STANDARD, TYPE_CLASS, TYPE_VENDOR
* @param descriptorType a {@link Descriptor}.TYPE_* value;
* the high order byte of the control request "value"
* @param id the ID (low order byte) in the control request "value"
* @param index the index parameter in the USB control request
* @param len the maximum amount of data to be returned
*
* @exception USBException reflecting a stall condition for
* devices not supporting the specified descriptor
*/
static byte [] getDescriptor (
Device dev,
byte descriptorClass,
byte descriptorType,
byte id,
int index,
int len
) throws IOException
{
byte buf [] = new byte [len];
ControlMessage msg = new ControlMessage ();
msg.setRequestType ((byte)(msg.DIR_TO_HOST
| descriptorClass
| msg.RECIPIENT_DEVICE
));
msg.setRequest (msg.GET_DESCRIPTOR);
msg.setValue ((short) ((descriptorType << 8) | (0xff & id)));
msg.setIndex ((short) index);
msg.setLength (len);
dev.control (msg);
return msg.getBuffer ();
}
/**
* Sets a descriptor on the specified device.
*
* @param descriptorClass TYPE_STANDARD, TYPE_CLASS, or TYPE_VENDOR
*/
public static void setDescriptor (
Device dev,
byte descriptorClass,
byte descriptorType,
byte id,
int index,
byte buf []
) throws IOException
{
if (index > 0xffff || buf.length > 0xffff)
throw new IllegalArgumentException ();
ControlMessage msg = new ControlMessage ();
msg.setRequestType ((byte)(msg.DIR_TO_DEVICE
| descriptorClass
| msg.RECIPIENT_DEVICE
));
msg.setRequest (msg.SET_DESCRIPTOR);
msg.setValue ((short) ((descriptorType << 8) | (0xff & id)));
msg.setIndex ((short) index);
msg.setBuffer (buf);
dev.control (msg);
}
/**
* Returns an array of languages supported by this device for
* its string descriptors, or null if no string descriptors
* are provided. These languages are identified by the numeric
* codes described in the USB 1.1 specification, which need
* mapping to Java locales if you intend to use them with other
* Java APIs.
*
* @see usb.util.LangCode#getLocale
*/
public static int [] getLanguages (Device dev)
throws IOException
{
byte buf [] = null;
try {
buf = getStandardDescriptor (dev,
Descriptor.TYPE_STRING,
(byte) 0, 0, 256);
} catch (USBException e) {
// devices without strings stall here
if (!e.isStalled ())
throw e;
}
if (buf == null || buf.length <4)
return null;
int len = 0xff & buf [0];
len >>= 1;
len -= 1;
if (len <= 0)
return null;
int retval [] = new int [len];
for (int i = 0; i < len; i++) {
int offset = 2 + (2 * i);
retval [i] = 0xff & buf [offset];
retval [i] += (0xff & buf [offset + 1]) << 8;
}
return retval;
}
/**
* Gets the specified string descriptor from the device.
* @param id Identifier for the string, any byte except zero
*/
public static String getString (
Device dev,
byte id,
int language
) throws IOException
{
byte buf [];
int len;
if (id == 0)
throw new IllegalArgumentException ();
buf = getStandardDescriptor (dev, Descriptor.TYPE_STRING,
id, language, 256);
if (buf.length < 2
|| buf [1] != Descriptor.TYPE_STRING
|| (len = 0x0ff & buf [0]) > buf.length
|| ((len -= 2) % 2) != 0
) {
return null;
}
// some JVMs won't handle UTF-16LE ("UnicodeLittle"); convert by hand
char data [] = new char [len >> 1];
for (int i = 0; i < data.length; i++) {
int j = (2 * i) + 2;
data [i] = (char) ((buf [j + 1] << 8) + (0x0ff & buf [j]));
}
return new String (data);
}
/**
* Returns the specified type of status.
* Device and endpoint status are described in section 9.4.5
* of the USB specification; bits are set and cleared using
* setFeature and clearFeature.
*
* @param dest includes a message type and recipient, such as masking
* the values TYPE_CLASS and RECIPIENT_INTERFACE.
* @param value typically zero
* @param index field for the feature, such as an endpoint (don't rely
* on seeing status for control endpoint zero) or hub port number
* @param len maximum size of returned array
*
* @exception USBException if another driver has claimed that
* interface or endpoint
*/
public static byte [] getStatus (
Device dev,
int dest,
int value,
int index,
int len
) throws IOException
{
ControlMessage msg = new ControlMessage ();
msg.setRequestType ((byte)(dest | DIR_TO_HOST));
msg.setRequest (msg.GET_STATUS);
msg.setValue ((short)value);
msg.setIndex ((short)index);
msg.setLength (len);
dev.control (msg);
return msg.getBuffer ();
}
/**
* Clears the identified feature flag to false.
* @see #setFeature
* @param dest includes a message type and recipient, such as masking
* the values TYPE_CLASS and RECIPIENT_OTHER.
* @param feature one of the device's defined feature identifiers
* @param index field for the feature, such as a hub port number (or zero)
*
* @exception USBException if another driver has claimed that
* interface or endpoint
*/
public static void clearFeature (
Device dev,
int dest,
int feature,
int index
) throws IOException
{
ControlMessage msg = new ControlMessage ();
msg.setRequestType ((byte)(dest & ~DIR_TO_HOST));
msg.setRequest (CLEAR_FEATURE);
msg.setValue ((short)(feature & 0xff));
msg.setIndex ((short)index);
dev.control (msg);
}
/**
* Sets the identified feature flag to true.
* This will retry in the face of stalls.
* @see #clearFeature
* @param dest includes a message type and recipient, such as masking
* the values TYPE_STANDARD and RECIPIENT_DEVICE.
* @param feature one of the device's defined feature identifiers
* @param index field for the feature, such as a hub port number (or zero)
*
* @exception USBException if another driver has claimed that
* interface or endpoint
*/
public static void setFeature (
Device dev,
int dest,
int feature,
int index
) throws IOException
{
ControlMessage msg = new ControlMessage ();
msg.setRequestType ((byte)(dest & ~DIR_TO_HOST));
msg.setRequest (SET_FEATURE);
msg.setValue ((short)(feature & 0xff));
msg.setIndex ((short)index);
dev.control (msg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -