libusb 读取“错误”数据

问题描述

目前我正在对我的新键盘的 USB 驱动程序进行逆向工程。 我捕获了一个“时钟请求”数据包,并尝试在我自己的 Linux 代码中实现它。 现在我在发送数据包时遇到问题,之后总是读取错误。 通常,当您发送“数据包” 0x11,0x84,0x00,....(64 字节)时,您应该得到一个答案,该答案也以 0x11,0x00 开头,长度为 64 字节,但我只得到回答 0x01,0x00...(64 字节)。

我还想知道我的代码如何发送和接收消息以及 Windows 如何发送和接收消息。

Windows Wireshark 捕获(通过 usbmon):

Windows Wireshark capture

Linux Wireshark 抓包(通过usbmon):

Linux Wireshark capture

我做错了吗?可以说libusb监听新数据吗?

当前代码

#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <memory.h>

#define VID 0x3282
#define PID 0x01

int main()
{
        libusb_context *ctx = NULL;
        libusb_device_handle *handle = NULL;
        int r;
        ssize_t cnt;
        r = libusb_init(&ctx);
        if (r < 0) {
                printf("Init error: %i}\n",r);
        }
        libusb_set_option(ctx,LIBUSB_OPTION_LOG_LEVEL,LIBUSB_LOG_LEVEL_DEBUG);

        handle = libusb_open_device_with_vid_pid(ctx,VID,PID);
        if (!handle) {
                perror("device not found\n");
                libusb_exit(ctx);
                return 1;
        }

        libusb_detach_kernel_driver(handle,3);

        r = libusb_claim_interface(handle,3);
        if (r < 0) {
                printf("Failed to claim interface: %i\n",r);
                libusb_exit(ctx);
                return 1;
        }

        unsigned char data[64];
        memset(data,64);
        data[0] = 0x11;
        data[1] = 0x84;

        int bytes_written;

        r = libusb_interrupt_transfer(handle,5,data,sizeof(data),&bytes_written,500);
        if (r != 0 || bytes_written != sizeof(data)) {
                printf("write Failed: %s\n",libusb_strerror(r));
                libusb_close(handle);
                libusb_exit(ctx);
                return 1;
        }

        printf("Wrote %i bytes\n",bytes_written);

        unsigned char read_data[64];
        memset(read_data,64);

        int bytes_read;

        r = libusb_interrupt_transfer(handle,132,read_data,sizeof(read_data),&bytes_read,500);
        if (r != 0 || bytes_read != sizeof(read_data)) {
                printf("read Failed: %s\n",libusb_strerror(r));
                libusb_close(handle);
                libusb_exit(ctx);
                return 1;
        }

        printf("Read %i bytes\n",bytes_read);
        printf("Data: ");
        for (int i = 0; i < bytes_read; i++) {
                printf("%02x",read_data[i]);
        }
        printf("\n");

        libusb_release_interface(handle,3);
        libusb_attach_kernel_driver(handle,3);
        libusb_close(handle);
        libusb_exit(ctx);
}

lsusb -v 端点描述符输出

[...]
Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x84  EP 4 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x05  EP 5 OUT
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)