我试图通过使用输出数据libusb_interrupt_transfer
,函数返回0
,但是,我看不到来自 Wireshark 的任何数据传输输出(在 USB 嗅探模式下),并且设备没有响应应该输出的数据。返回值是否0
表示数据是从usb口发出来的?如果是这样,我是否libusb_interrupt_transfer
在我的代码上使用了错误的函数?
这是我的代码(我在 ubuntu 12.10 - Linux ubuntu 3.5.0-21-generic #32-Ubuntu SMP Tue Dec 11 18:52:46 UTC 2012 i686 i686 i686 GNU/Linux 上运行代码)
#include <stdio.h> //printf, etc.
#include <sys/types.h>
#include <unistd.h>
#include <libusb.h> //libusb functions
#include <inttypes.h> //unit16t
#include <unistd.h> //sleep function
#include <stdlib.h> //exit()
const uint16_t VENDOR = 6069; /* = 0x17b5 */
const uint16_t PRODUCT = 32; /* = 0x0020 */
const int CONFIGURATION_VALUE = 1;
int msgsend(unsigned char data[64]) {
libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
libusb_context *ctx = NULL; //a libusb session
int r; //for return values
ssize_t cnt; //holding number of devices in list
r = libusb_init(&ctx); //initialize a library session
if (r < 0){
exit(1);}
libusb_set_debug(ctx, 3);//set verbosity level to 3
cnt = libusb_get_device_list(ctx, &devs);
if (cnt < 0)
return (int) cnt;
libusb_device_handle *handle;
handle = libusb_open_device_with_vid_pid(ctx, VENDOR, PRODUCT);
if (handle==NULL) {
fprintf(stderr, "No logging system found.\n");
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
exit(1);
}
libusb_device *device = libusb_get_device(handle);
struct libusb_config_descriptor *config = NULL;
int err = libusb_get_config_descriptor_by_value(device, CONFIGURATION_VALUE, &config);
if (err != LIBUSB_SUCCESS){
printf("libusb_get_config_descriptor_by_value error\n");
exit(1);
}
if(config->bNumInterfaces == 1){
if(config->interface[0].num_altsetting ==1){
if(config->interface[0].altsetting[0].bNumEndpoints == 2) {
libusb_free_config_descriptor(config);
err = libusb_kernel_driver_active(handle, 0);
if (err >= LIBUSB_SUCCESS) {
if (err == 1) {
printf("Kernel driver is active, trying to detach\n");
err = libusb_detach_kernel_driver(handle, 0);
if (err != LIBUSB_SUCCESS) {
printf("Error detaching interface from kernel: libusb_error_string(err)\n");
}
}
err = libusb_set_configuration(handle, CONFIGURATION_VALUE);
if (err == LIBUSB_SUCCESS) {
err = libusb_claim_interface(handle, 0);
if (err == LIBUSB_SUCCESS) {
err = libusb_set_interface_alt_setting(handle, 0, 0);
if (err == LIBUSB_SUCCESS) {
unsigned char ENDPOINT_OUTPUT = 0x02;
unsigned char ENDPOINT_INPUT = 0x81;
int actual_length;
int return_value = 0;
return_value = libusb_interrupt_transfer(handle, ENDPOINT_OUTPUT, data, sizeof(data), &actual_length, 0);
if (return_value == 0){
printf("msg was sent successfully! return_value=%d\n",return_value);
}else {
printf("return_value=%d, something went wrong",return_value);
perror( "libusb_submit_transfer\n" );
}
}
}else{
printf("cannot claim the interface\n");
exit(1);
}
}
}
}
}
}
libusb_free_device_list(devs, 1); //free the list, unref the devices in it
r = libusb_release_interface(handle, 0);
libusb_close(handle); //close the device we opened
libusb_exit(ctx);//needs to be called to end the
return 0;
}
int main(){
unsigned char data[64]={0x12, 0x01,0x00,0x03,0x00,0x00,0xFF,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
unsigned char data2[64]={0x12, 0x02,0x00,0x03,0x00,0x00,0xFF,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned int seqnum = 0x01;
while(1){
data[1] = seqnum;
printf("max light is set\n");
msgsend(data);
if(seqnum == 0xff){
seqnum = 0;
}else{
seqnum++;
}
sleep(2);
data2[1] = seqnum;
printf("min light is set\n");
msgsend(data2);
if(seqnum == 0xff){
seqnum = 0;
}else{
seqnum++;
}
sleep(2);
}
return 0;
}