gpt4 book ai didi

c++ - 如何将成员函数指针传递给 libusb?

转载 作者:太空狗 更新时间:2023-10-29 22:54:58 25 4
gpt4 key购买 nike

我正在编写一个简单的库,它使用 libusb 连接到我的自定义硬件,每 50 毫秒向主机设备发送一次信号。它旨在提供一个简单的抽象层,以便用户根本不会为 libusb 所困扰。我需要将指向非静态类成员函数的指针传递给 libusb_fill_bulk_transfer . IE。我需要为 MyDevice 的每个实例单独回调,而不向我的用户公开 libusb 逻辑。

基本设计是:

  • 初始化库

    int mylib_init(){  libusb_init(&context);}
  • find all compatible devices

    int mylib_get_valid_devices(vector devs){ //... libusb_device **devs1; int countAll = libusb_get_device_list(context, &devs1); //... fill devs}int mylib_print_device_info(MyDevice* dev); 
  • connect to as many devices as the user likes

    int mylib_init_device(MyDevice* dev){   libusb_open(dev->device, &dev->dev_handle);   // check for kernel driver & remove if needed   libusb_claim_interface(dev->dev_handle, 0);   //...}
  • set up callbacks for incoming data to all instances of connected devices

    int mylib_start_transmission_async(MyDevice* dev, MyLibCallbackIn user_cb, unsigned char* buffer_in, int bufferSize){   libusb_control_transfer(dev->dev_handle, p1, p2, p3, p4, p5, p6, p7);   // .. rest of libusb_control_transfer -s   //start device:   int actual;   unsigned char startSig = 0b00110111;   libusb_bulk_transfer(dev->dev_handle, (1 | LIBUSB_ENDPOINT_OUT), &tosend, 1, &actual, 100);   dev->user_callback = user_cb;   dev->buffer_in = buffer_in;    dev->bufferSize = bufferSize;   //setup transfer   dev->transfer_in = libusb_alloc_transfer(0);   libusb_fill_bulk_transfer(dev->transfer_in, dev->dev_handle, LIBUSB_ENDPOINT_IN, dev->buffer_in,dev->bufferSize, cb_in, NULL, 0);   libusb_submit_transfer(dev->transfer_in); } 
  • disconnect when finished

    int mylib_disconnect_device(MyDevice* device);

  • exit the lib

    int mylib_exit();

MyDevice is defined as:

typedef int (*MyLibdCallbackIn)(unsigned char* buffer, int length);

class MyDevice{

unsigned char* buffer_in;
unsigned char* buffer_out;

libusb_device* device = nullptr;
libusb_device_handle* dev_handle = nullptr;

struct libusb_transfer* transfer_in = nullptr;
struct libusb_transfer* transfer_out = nullptr;

//this obviously wouldn't work because libusb doesn't accept it as a param:
void LIBUSB_CALL cb_in(struct libusb_transfer* transfer);
MyLibCallbackIn user_callback;

void run();
//...

}

cb_in 定义为:

void LIBUSB_CALL MyDevice::cb_in(libusb_transfer* transfer){
int r = libusb_submit_transfer(this->transfer_in);
callback_in(transfer->buffer, transfered);

}

我无法将 MyDevice::cb_in 传递给 libusb_fill_bulk_transfer,因为函数指针类型不兼容。同时,我不希望我的用户必须编写一个回调函数,将 libusb_transfer* 作为参数(暴露给 libusb)直接传递给 libusb_fill_bulk_transfer.

编辑:我试过

void LIBUSB_CALL callback_wrapper(struct libusb_transfer* transfer){
MyDevice* dev = reinterpret_cast<MyDevice*>(transfer->user_data);
dev->cb_in(transfer);
}

但是出现Sementation fault (Core dumped)错误

最佳答案

根据 Max Langhof 和 Sam Varshavchik 的评论,解决方案是将 MyDevice 的实例传递给 libusb_fill_bulk_transfer。所以:

int mylib_start_transmission_async(MyDevice* dev, MyLibCallbackIn user_cb, unsigned char* buffer_in, int bufferSize){

//...
// setup transfer:
dev->transfer_in = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(dev->transfer_in, dev->dev_handle, LIBUSB_ENDPOINT_IN, dev->buffer_in,dev->bufferSize, cb_in, dev, 0);
libusb_submit_transfer(dev->transfer_in); ^^^

}

然后像编辑中那样使用回调包装器。谢谢!

关于c++ - 如何将成员函数指针传递给 libusb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com