2015-04-06 204 views
2

我正在使用通过DATA管道接收命令的设备(wiimote),并且只接受与命令本身一样长的命令数据包。例如,它会接受:hidapi:发送数据包小于caps.OutputReportByteLength

0x11 0x10 

但它不会接受:

0x11 0x10 0x00 0x00 0x00 ... etc. 

这是Windows上的问题,因为WriteFile的()的窗口上要求字节[]传递给它是至少与caps.OutputReportByteLength一样长。在Mac上,这个限制不存在,我的代码正常工作。下面是从hid.c导致此问题的代码:

/* Make sure the right number of bytes are passed to WriteFile. Windows 
    expects the number of bytes which are in the _longest_ report (plus 
    one for the report number) bytes even if the data is a report 
    which is shorter than that. Windows gives us this value in 
    caps.OutputReportByteLength. If a user passes in fewer bytes than this, 
    create a temporary buffer which is the proper size. */ 
if (length >= dev->output_report_length) { 
    /* The user passed the right number of bytes. Use the buffer as-is. */ 
    buf = (unsigned char *) data; 
} else { 
    /* Create a temporary buffer and copy the user's data 
     into it, padding the rest with zeros. */ 
    buf = (unsigned char *) malloc(dev->output_report_length); 
    memcpy(buf, data, length); 
    memset(buf + length, 0, dev->output_report_length - length); 
    length = dev->output_report_length; 
} 
res = WriteFile(dev->device_handle, buf, length, NULL, &ol); 

卸下上面的代码,如在评论中提到的,在一个错误的结果来自WriteFile的()。

有什么办法可以将数据传递给任意大小的设备?预先感谢您的帮助。

回答

1

已解决。我使用了类似于Wii仿真器Dolphin的解决方案。显然,在Microsoft蓝牙堆栈上,WriteFile()无法正常工作,导致Wiimote返回错误。通过使用MS堆栈上的HidD_SetOutputReport()和BlueSoleil堆栈上的WriteFile(),我能够成功连接到设备(至少在我的机器上)。

我还没有在BlueSoleil堆栈上测试过这个,但是Dolphin使用这种方法,所以可以说它是安全的。

这里是一个包含这个修复丑陋实现的要点: https://gist.github.com/Flafla2/d261a156ea2e3e3c1e5c