I am trying to send raw data to an HID Device (NTAG NFC Tag)
using the Usb4Java Api. ANY HELP would be greatly appreciated! My code connects to the interface and
LibUsb.interruptTransfer(handle, outEndpoint, buffer, transferred, timeout) == LibUsb.SUCCESS.
Which I thought meant that the interrupt transfer has been a success? But, the data is not being stored on the chip... Do I need to contact the Vendor? Is the Api working correctly? Any help would be awesome!
private static final byte[] MESSAGE = {0x01, 0x21, 0x21, 0x21, 0x21, 0x21,
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21};
/**NFC Device Info**/
private static final short vendorID = 0x1fc9;
private static final short productID = 0x0088;
private static final byte Interface = 0;
private static final byte outEndpoint = (byte)0x01;
private static final int timeout = 618;
public void transfer() {
System.out.println("Sync Transfer started...");
// Initialize the libusb context
int result = LibUsb.init(null);
if (result != LibUsb.SUCCESS)
{
System.err.println("Context cannot be initialized.");
throw new LibUsbException(result);
} else {
System.out.println("Context Initialized");
}
// Open test device (NXP)
DeviceHandle handle = LibUsb.openDeviceWithVidPid(null, vendorID,
productID);
if (handle == null)
{
System.err.println("Test device not found.");
System.exit(1);
} else {
System.out.println("Device Found");
}
// Claim the interface
result = LibUsb.claimInterface(handle, Interface);
if (result != LibUsb.SUCCESS)
{
System.err.println("Unable to claim interface");
throw new LibUsbException(result);
} else {
System.out.println("Interface Claimed");
}
// Send Message
write(handle, message);
// Release the interface
result = LibUsb.releaseInterface(handle, Interface);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to release interface", result);
}
// Close the device
LibUsb.close(handle);
// Deinitialize the libusb context
LibUsb.exit(null);
}
public void write(DeviceHandle handle, byte[] data)
{
ByteBuffer buffer = BufferUtils.allocateByteBuffer(64);
buffer.put(data);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
buffer.rewind();
int result = LibUsb.interruptTransfer(handle, outEndpoint, buffer, transferred, timeout);
if (result != LibUsb.SUCCESS)
{
System.err.println("Unable to send data!");
throw new LibUsbException(result);
} else {
System.out.println("Data Written");
}
}
Output:
Sync Transfer started...
Context Initialized
Device Found
Interface Claimed
Data Written
Lastly, my message format comes from this C program that writes to the same chip (and WORKS) over 12c using a 17 byte message [adress, byte1, ... byte16].
int NTAGI2C_writeMemory(int dev_addr,unsigned char mem_addr,unsigned char mem_data[])
{
struct i2c_rdwr_ioctl_data i2c_data;
struct i2c_msg msg[2];
unsigned char buffer[17];
buffer[0] = mem_addr;
memcpy(buffer+1, mem_data,16);
i2c_data.msgs = msg;
i2c_data.nmsgs = 1;
i2c_data.msgs[0].addr = dev_addr;
i2c_data.msgs[0].flags = 0;
i2c_data.msgs[0].len = 17;
i2c_data.msgs[0].buf = (__u8*)buffer;
int ret = ioctl(fd,I2C_RDWR,&i2c_data);
if(ret<0)
{
perror("write data fail\n");
return 0;
}
usleep(6);
return 1;
}