#include <iostream>
#include <libusb.h>

bool isFastbootDevice(libusb_device *device) {
    libusb_device_handle *handle = nullptr;
    int result = libusb_open(device, &handle);
    if (result != LIBUSB_SUCCESS) {
        std::cerr << "Error opening device" << std::endl;
        return false;
    }

    uint8_t buffer[256];
    result = libusb_get_device_descriptor(device, reinterpret_cast<libusb_device_descriptor *>(buffer));
    if (result != LIBUSB_SUCCESS) {
        std::cerr << "Error getting device descriptor" << std::endl;
        libusb_close(handle);
        return false;
    }

    libusb_device_descriptor *desc = reinterpret_cast<libusb_device_descriptor *>(buffer);

    // 检查设备ID和厂商字符串是否为fastboot设备的标志
    bool isFastbootDevice = (desc->idVendor == 0x18d1 && desc->idProduct == 0x4e26) ||
                            (desc->idVendor == 0x0bb4 && desc->idProduct == 0x0fff);

    libusb_close(handle);
    return isFastbootDevice;
}

int main() {
    libusb_context *context = nullptr;
    int result = libusb_init(&context);
    if (result != LIBUSB_SUCCESS) {
        std::cerr << "Failed to initialize libusb" << std::endl;
        return -1;
    }

    libusb_device **devs;
    ssize_t cnt = libusb_get_device_list(context, &devs);
    if (cnt < 0) {
        std::cerr << "Error retrieving device list" << std::endl;
        libusb_exit(nullptr);
        return -1;
    }

    for (int i = 0; i < cnt; i++) {
        libusb_device *device = devs[i];
        if (isFastbootDevice(device)) {
            std::cout << "Fastboot device found" << std::endl;
        } else {
            std::cout << "Not a fastboot device" << std::endl;
        }
    }

    libusb_free_device_list(devs, 1);
    libusb_exit(context);
    return 0;
}
分类: usb

0 条评论

发表回复

您的电子邮箱地址不会被公开。