Note that there are some explanatory texts on larger screens.

plurals
  1. POUSB Mass Storage linux
    primarykey
    data
    text
    <p>I am new in linux device driver. I wan to write a C/C++ code to perform file transfer from raspberry pi to usb flash drive. I having difficult for the starting point, so i try on libusb for HID device sample code from signal11 and the code works fine for detecting my optical mouse with its device ID. Then i try to obtain usb flash drive vendor id somehow it give me very wired number. Finally i come out with a very silly try out by writing a bash script for cp a file to usb flash drive and activate the script in C++ and it works but i feel it is not a proper way to do it. Then i start with SCSI protocol and i very hard to understand how it works.Any guideline is appreciated.</p> <pre><code>int scsi_get_serial(int fd, void *buf, size_t buf_len) { // we shall retrieve page 0x80 as per http://en.wikipedia.org/wiki/SCSI_Inquiry_Command unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0}; unsigned char sense[32]; struct sg_io_hdr io_hdr; int result; memset(&amp;io_hdr, 0, sizeof (io_hdr)); io_hdr.interface_id = 'S'; io_hdr.cmdp = inq_cmd; io_hdr.cmd_len = sizeof (inq_cmd); io_hdr.dxferp = buf; io_hdr.dxfer_len = buf_len; io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; io_hdr.sbp = sense; io_hdr.mx_sb_len = sizeof (sense); io_hdr.timeout = 5000; result = ioctl(fd, SG_IO, &amp;io_hdr); if (result &lt; 0) return result; if ((io_hdr.info &amp; SG_INFO_OK_MASK) != SG_INFO_OK) return 1; return 0; } int main(int argc, char** argv) { //char *dev = "/dev/sda"; char *dev = "/dev/sg2"; char scsi_serial[255]; int rc; int fd; fd = open(dev, O_RDONLY | O_NONBLOCK); if (fd &lt; 0) { perror(dev); } memset(scsi_serial, 0, sizeof (scsi_serial)); rc = scsi_get_serial(fd, scsi_serial, 255); // scsi_serial[3] is the length of the serial number // scsi_serial[4] is serial number (raw, NOT null terminated) if (rc &lt; 0) { printf("FAIL, rc=%d, errno=%d\n", rc, errno); } else if (rc == 1) { printf("FAIL, rc=%d, drive doesn't report serial number\n", rc); } else { if (!scsi_serial[3]) { printf("Failed to retrieve serial for %s\n", dev); return -1; } printf("Serial Number: %.*s\n", (size_t) scsi_serial[3], (char *) &amp; scsi_serial[4]); } close(fd); return (EXIT_SUCCESS); } </code></pre> <p>I get this serial number: 00/1F</p> <p>Then i try write this in test.sh</p> <pre><code>cp /home/Desktop/stl4.pdf /media/mini_flash </code></pre> <p>and run system("./test.sh") in C++</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload