I’m trying to send a message to our custom hid device and not getting a response. The message requests the device’s current state, which causes the device to output the state of all its knobs, buttons, etc.
[StructLayout( LayoutKind.Explicit, Size = size )]
public unsafe struct GetDeviceStateMessage : IInputDeviceCommandInfo
{
public static FourCC type => new FourCC( 'H', 'I', 'D', 'O' );
internal const int size = InputDeviceCommand.BaseCommandSize + 3;
internal const int id = 63;
internal const int length = 7;
internal const int message = 255;
[FieldOffset(0)]
public InputDeviceCommand baseCommand;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 0)]
public byte reportId;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 2)]
public byte reportLength;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 3)]
public byte getDeviceStateMessage;
public FourCC typeStatic => type;
public static GetDeviceStateMessage Create()
{
return new GetDeviceStateMessage
{
baseCommand = new InputDeviceCommand( type, size ),
reportId = id,
reportLength = length,
getDeviceStateMessage = message
};
}
}
Its called when a device is reconnected:
InputSystem.onDeviceChange +=
( device, change ) =>
{
if( device.GetType() != typeof( My_HID_Device ) )
return;
switch( change )
{
case InputDeviceChange.Reconnected:
My_HID_Device.current.RequestDeviceState();
break;
}
};
which calls:
public void RequestDeviceState()
{
Debug.Log( "REQUEST" );
var command = GetDeviceStateMessage.Create();
ExecuteCommand( ref command );
}
On a previous platform I had packed the message into a 64 byte packet and sent it out by hand like this:
bool HandHeldInput::MessageDevice( BYTE msgIn[], int msgLength ) {
bool SUCCESS = true;
BYTE buff[USB_OUTPUT_SIZE];
memset( buff, 0x0, USB_OUTPUT_SIZE );
for( int i = 0; i < msgLength; ++i )
buff[i] = msgIn[i];
if( m_hDeviceHandle == NULL ||
hid_write( m_hDeviceHandle, buff, USB_OUTPUT_SIZE ) == -1 )
{
std::cout << "Device message not sent! Is message format correct?" << std::endl;
SUCCESS = false;
}
return SUCCESS;
}
I’m not sure if something different is going on under the hood for Unity and the base command?
I also notice that if I set a break point and connect the debugger and run (the game) - I stop receiving device input, though I still see them coming across in the Input Debug editor.