bzkrtmurat
bzkrtmurat

Reputation: 189

Getting raw data using libusb

I'm doing reverse engineering about a ultrasound probe on the Linux side. I want to capture raw data from an ultrasound probe. I'm programming with C and using the libusb API.

There are two BULK IN endpoints in the device (2 and 6). The device is sending 2048 bytes data, but it is sending data as 512 bytes with four block.

Enter image description here

This picture is data flow on the Windows side, and I want to copy that to the Linux side. You see four data blocks with endpoint 02 and after that four data blocks with endpoint 06.

But there is a problem about timing. The first data block of endpoint 02's and first data block of endpoint 06's are close to each other acoording to time. But in data flow they are not in sequence.

I see that the computer is reading the first data blocks of endpoint 02 and 06. After that, the computer is reading the other three data blocks of endpoint 02 and endpoint 06. But in USB Analyzer, the data flow is being viewed according to the endpoint number. The sequence is different according to time.

On the Linux side, I write code like this:

int index = 0;

imageBuffer2 = (unsigned char *) malloc(2048);
imageBuffer6 = (unsigned char *) malloc(2048);

while (1) {
    libusb_bulk_transfer(devh, BULK_EP_2, imageBuffer2, 2048, &actual2, 0);

    libusb_bulk_transfer(devh, BULK_EP_6, imageBuffer6, 2048, &actual6, 0);

    //Delay
    for(index = 0; index <= 10000000; index ++)
    {

    }
}

So that result is in picture as below

Enter image description here

In other words, in my code all reading data is being read in sequence according to time and endpoint number. My result is different from the data flow on the Windows side.

In brief, I have two BULK IN endpoints, and they are starting read data close according to time. How is it possible?

Upvotes: 3

Views: 2458

Answers (1)

Jaciq
Jaciq

Reputation: 158

It's not clear to me whether you're using a different method for getting the data on Windows or not, I'm going to assume that you are.

I'm not an expert on libusb by any means, but my guess would be that you are overwriting you data with each call, since you're using the same buffer each time. Try giving your buffer a fixed value before using the transfer method, and then evaluate the result. If it is the case, I believe something along the lines of the following would also work in C:

imageBuffer2 = (unsigned char *) malloc(2048);
char *imageBuffer2P = imageBuffer2;
imageBuffer6 = (unsigned char *) malloc(2048);
char *imageBuffer6P = imageBuffer6;
int dataRead2 = 0;
int dataRead6 = 0;

while(dataRead2 < 2048 || dataRead6 < 2048)
{
  int actual2 = 0;
  int actual6 = 0;
  libusb_bulk_transfer(devh, BULK_EP_2, imageBuffer2P, 2048-dataRead2, &actual2, 200);
  libusb_bulk_transfer(devh, BULK_EP_6, imageBuffer6P, 2048-dataRead6, &actual6, 200);
  dataRead2 += actual2;
  dataRead6 += actual6;
  imageBuffer2P += actual2;
  imageBuffer6P += actual6;
  usleep(1);
}

Upvotes: 2

Related Questions