0

I am running Ubuntu 12.04 compiling a c++ file which has the following code

#include <iostream>
#include <libusb-1.0/libusb.h>

using namespace std;

int main(){
    //pointer to pointer of device used to retrieve a list of devices
    libusb_device **devs;
    libusb_device_handle *dev_handle; //a device handle
    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 the library for the session we just declared

    if(r < 0){
        cout <<"init error "<<r<< endl;
        return 1;
    }

    libusb_set_debug(ctx, 3); // set verbosity level to 3, as suggested in the documentation
    cnt = libusb_get_device_list(ctx, &devs); //get the list of devices

    if (cnt < 0) {
        cout <<"Get Device Error "<< endl; // there was an error
        return 1;
    }

    cout << cnt <<" Device in list " << endl;
    //dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); // these are vendor id and product id   //simon's usb(duracell)1516:1213
    dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); //these are vendorID and productID I found for my usb device

    if (dev_handle == NULL){
        cout <<"Cannot open device "<< endl;
    }else{
        cout << "Device opened" << endl;
    }

    libusb_free_device_list(devs, 1);// free the list unref the devices in it

    unsigned char *data = new unsigned char[4];//data to write
    data[0] = 'a'; data[1] = 'b'; data[2] = 'c'; data[3] = 'd';//some dummy values

    int actual; //used to find how many bytes were written

    if (libusb_kernel_driver_active(dev_handle, 0) == 1){// findout if kernal driver attached
        cout << "Kernal Driver Active" << endl;
        if (libusb_detach_kernel_driver(dev_handle, 0) == 0 ){  //detach it
            cout<< "Kernal Driver Detached" << endl;
        }
    }

    r = libusb_claim_interface(dev_handle, 0);// claim interface 0 (the first) of devices

    if(r < 0){
        cout <<"Cannot claim interface "<<endl;
        return 1;
    }

    cout <<"Claimed interface "<<endl;

    cout<<"data->"<<data<<"<-"<<endl; // just to see the data we want to write : abcd
    cout<<"Writing data..."<<endl;

    r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);//my device's out endpoint was 2, found withe trial - the device had two endpoints: 2 and 129

    if(r == 0 && actual == 4){  // we wrote 4 bytes successfully
        cout<<"Writing successfull"<<endl;
    }else{
        cout<<"write error"<<endl;
    }

    r = libusb_release_interface(dev_handle, 0); // release the claimed interface

    if(r!=0) {
        cout<<"Cannot Release Interface"<<endl;
        return 1;
    }
    cout<<"Released interface"<<endl;
    libusb_close(dev_handle); // close the device we opened
    libusb_exit(ctx); // need to be called to end the

    delete[] data;// delete the allocated memory for data
    return 0;
}

but when i compile the above code using the following command line (these are the product id and vendor id of my usb)

dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689);

the compiler throws the following error

transfer_data_libusb.cpp:30:55: error: invalid digit "9" in octal constant

Someone had adviced me to remove the leading zero ie ("951" instead of "0951") but when i do that, the file gets compiled successfully, but when i rnu the compiled version this throws the following error

7 Device in list 
Cannot open device 
Segmentation fault (core dumped)

dont know what should I do can you please help btw i use the following command to compile the above code

g++ transfer_data_libusb.cpp $(pkg-config --libs libusb-1.0) -o transfer_data_libusb

thank you very much for you time

Deanie
  • 2,316
  • 2
  • 19
  • 35
Amjad
  • 1,950
  • 5
  • 26
  • 41
  • Seems you are trying to access a noninitialized memory-location. Maybe a null-reference somewhere? Have you debugged you code? For you are working in user-space you can debug it. – bash.d Feb 26 '13 at 11:23

2 Answers2

3

Your number is probably in hexadecimal. Use:

0x951

instead of your 0951.

And:

0x1689

instead of your 1689.

If you see the other numbers in (I assume it is your product):

http://usbspeed.nirsoft.net/?g=32gb

they have a to f characters, so it means the format should be hexadecimal.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Same for 1689 it should be 0x1689 – ismail Feb 26 '13 at 11:23
  • thanks folks that work compiled the code but it throwing some other errors now which are (libusb couldn't open open USB device permission denied) can you help me get to the bottom of this please i am newbie in c c++ and libusb – Amjad Feb 26 '13 at 11:48
  • Following errors appears on terminal when I compiled the code 7 Device in list libusb:error [op_open] libusb couldn't open USB device /dev/bus/usb/002/003: Permission denied. libusb:error [op_open] libusb requires write access to USB device nodes. Cannot open device Segmentation fault (core dumped) – Amjad Feb 26 '13 at 11:49
0

When your call fails

dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); 

dev_handle will be NULL.

if (dev_handle == NULL){
    cout <<"Cannot open device "<< endl;
    //treat error here and quit

You need to stop operating on dev_handle then.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • thanks buddy but the device is there when I change the above code (0x951 and 0x1689) I got another error which was permission denied to be precise with the error it was (7 Device in list libusb:error [op_open] libusb couldn't open USB device /dev/bus/usb/002/003: Permission denied. libusb:error [op_open] libusb requires write access to USB device nodes. Cannot open device Segmentation fault (core dumped)) – Amjad Feb 26 '13 at 12:09
  • Are you using libusb as root? – bash.d Feb 26 '13 at 12:22
  • sorry for the above comments by mistake yes I logged in as a root user and it works but i cant go to my usb device physically but wheni typed lsusb i can see it is there? – Amjad Feb 26 '13 at 12:41
  • Try nout to use a `libusb_context` - object, i.e. `libusb_open_device_with_vid_pid ( NULL, 0x951, 0x1689 )` – bash.d Feb 26 '13 at 12:47
  • no it does not work i tried but same error (Segmentation fault (core dumped))... when i am running as a root user as soon as I execute the (./transfer_data_libusb) command usb disappears... can see this any more... even though the message was write successfully... i want to see the written data (abcd) but cant see...??? – Amjad Feb 26 '13 at 13:09