Search code examples
javaclinuxportingscsi

Java scsi access


I'd like to port a Linux C program to Java. This program controls a camera which is connected to the PC with a USB cable. The C code uses Linux SCSI Generic (sg).

Sample code from the C program:

#include <linux/../scsi/sg.h>

...

static int scsi_write(int sg_fd, uint8_t *cmd, uint32_t cmdLen,
               uint8_t *buf, uint32_t bufLen) {

    sg_io_hdr_t io;
    int r;

    memset(&io, 0, sizeof(io));

    io.interface_id = 'S';
    io.cmd_len = cmdLen;

    ...        
    r = ioctl(sg_fd, SG_IO, &io);
    ...
}

Is there a way to port this program to Java? I was searching for a cross-platform SCSI library written for Java, but found none. I was also searching for a JNI over SCSI/sg, also no luck.


Solution

  • While Java supports a lot of the POSIX API, the ioctl system call is not part of what it does. What you'll need to do is to use JNI to allow Java to call a function such as the scsi_write you wrote in the question. The extra cost of using more shims is minimal given that you're talking about interfacing to external hardware anyway. The cmd and buf arguments map naturally to Java byte arrays (and since Java's arrays know their length, you won't model the cmdLen and bufLen arguments at the Java level at all).