在 libusb_fill_interrupt_transfer 回调中使用 "user_data" 时出错
Error when using "user_data" in libusb_fill_interrupt_transfer callback
我正在使用 libusb-1.0 将 BLE 加密狗与 RCU 配对。
为此,我成功向dongle接口写入配对请求。
为了收听加密狗响应,我使用函数 libusb_fill_interrupt_transfer
并传递一个回调,该回调将在收到响应时执行。
此函数接受一个参数,如文档 (void *user_data
) 中所述,可在回调中使用。但是当我尝试使用这个参数时,我得到一个编译错误。
undeclared (first use in this function)
继上一个函数的调用和我的回调声明之后:
libusb_fill_interrupt_transfer(pairing->transfer, dctx->devh, 0x84, pairing->buffer,
sizeof(pairing->buffer), cb_aknowledgement, pairing, 0);
static void cb_aknowledgement(struct libusb_transfer *transfer)
{
if (pairing->transfer->status != LIBUSB_TRANSFER_COMPLETED) {
printf( "img transfer status %d?\n", pairing->transfer->status);
libusb_free_transfer(pairing->transfer);
pairing->transfer = NULL;
return;
}
if(pairing->buffer[0]!=0x05 || pairing->buffer[1]!=0x21)
{
printf( "wrong command recieved\n");
libusb_free_transfer(pairing->transfer);
pairing->transfer = NULL;
return;
}
printf("I've read data \n");
printf("USB Report Id = 0x%x \n",pairing->buffer[0]);
printf("Command = 0x%x \n",pairing->buffer[1]);
printf("Acknowledgement type = 0x%x \n",pairing->buffer[2]);
return ;
}
问题是:如何使用作为参数传递给回调的 user_data
?
使用transfer->user_data
。来自 libusb_transfer structure doc :
Data Fields
void * user_data
User context data to pass to the callback function.
我不知道 pairing
的类型是什么,但它看起来像这样:
int main() {
...
struct pairing_type_s *pairing = pairing_init();
...
libusb_fill_interrupt_transfer(pairing->transfer, dctx->devh, 0x84, pairing->buffer,
sizeof(pairing->buffer), cb_aknowledgement, pairing, 0);
...
}
// Then later:
static void cb_aknowledgement(struct libusb_transfer *transfer)
{
assert(transfer != NULL);
struct pairing_type_s *pairing = transfer->user_data;
assert(pairing != NULL);
// use pairing like a pro
...
}
但你也可以更专业,如果你确保你总是用 pairing->transfer
和 cb_aknowledgement
调用 libusb_fill_interrupt_transfer
并使用 container_of
宏:
int main() {
...
struct pairing_type_s *pairing = pairing_init();
...
libusb_fill_interrupt_transfer(pairing->transfer, dctx->devh, 0x84, pairing->buffer,
sizeof(pairing->buffer), cb_aknowledgement, NULL, 0);
...
}
// Then later:
static void cb_aknowledgement(struct libusb_transfer *transfer)
{
assert(transfer != NULL);
struct pairing_type_s *pairing = container_of(transfer, struct pairing_type_s, transfer);
assert(pairing != NULL);
// use pairing like a pro
...
}
但在这种情况下我更喜欢第一种方法,因为它更易读且更不会出错。
我正在使用 libusb-1.0 将 BLE 加密狗与 RCU 配对。
为此,我成功向dongle接口写入配对请求。
为了收听加密狗响应,我使用函数 libusb_fill_interrupt_transfer
并传递一个回调,该回调将在收到响应时执行。
此函数接受一个参数,如文档 (void *user_data
) 中所述,可在回调中使用。但是当我尝试使用这个参数时,我得到一个编译错误。
undeclared (first use in this function)
继上一个函数的调用和我的回调声明之后:
libusb_fill_interrupt_transfer(pairing->transfer, dctx->devh, 0x84, pairing->buffer,
sizeof(pairing->buffer), cb_aknowledgement, pairing, 0);
static void cb_aknowledgement(struct libusb_transfer *transfer)
{
if (pairing->transfer->status != LIBUSB_TRANSFER_COMPLETED) {
printf( "img transfer status %d?\n", pairing->transfer->status);
libusb_free_transfer(pairing->transfer);
pairing->transfer = NULL;
return;
}
if(pairing->buffer[0]!=0x05 || pairing->buffer[1]!=0x21)
{
printf( "wrong command recieved\n");
libusb_free_transfer(pairing->transfer);
pairing->transfer = NULL;
return;
}
printf("I've read data \n");
printf("USB Report Id = 0x%x \n",pairing->buffer[0]);
printf("Command = 0x%x \n",pairing->buffer[1]);
printf("Acknowledgement type = 0x%x \n",pairing->buffer[2]);
return ;
}
问题是:如何使用作为参数传递给回调的 user_data
?
使用transfer->user_data
。来自 libusb_transfer structure doc :
Data Fields
void * user_data
User context data to pass to the callback function.
我不知道 pairing
的类型是什么,但它看起来像这样:
int main() {
...
struct pairing_type_s *pairing = pairing_init();
...
libusb_fill_interrupt_transfer(pairing->transfer, dctx->devh, 0x84, pairing->buffer,
sizeof(pairing->buffer), cb_aknowledgement, pairing, 0);
...
}
// Then later:
static void cb_aknowledgement(struct libusb_transfer *transfer)
{
assert(transfer != NULL);
struct pairing_type_s *pairing = transfer->user_data;
assert(pairing != NULL);
// use pairing like a pro
...
}
但你也可以更专业,如果你确保你总是用 pairing->transfer
和 cb_aknowledgement
调用 libusb_fill_interrupt_transfer
并使用 container_of
宏:
int main() {
...
struct pairing_type_s *pairing = pairing_init();
...
libusb_fill_interrupt_transfer(pairing->transfer, dctx->devh, 0x84, pairing->buffer,
sizeof(pairing->buffer), cb_aknowledgement, NULL, 0);
...
}
// Then later:
static void cb_aknowledgement(struct libusb_transfer *transfer)
{
assert(transfer != NULL);
struct pairing_type_s *pairing = container_of(transfer, struct pairing_type_s, transfer);
assert(pairing != NULL);
// use pairing like a pro
...
}
但在这种情况下我更喜欢第一种方法,因为它更易读且更不会出错。