I'm reading binary data from the device using usb4java asynchronous method . My application receives input from other application when client send start reading binary data 1st time everything works fine. What when the client sends stop and calling start again it is not working as expected. Below is my code i call when i receive start
if(toggle.equalsIgnoreCase("stop"))
{
exit=true;
}
if(toggle.equalsIgnoreCase("start"))
{
exit=false;
}
if(!exit) {
thortelem.initlibusb();
//Device device=thortelem.findDevice(vendorID, productID);
handle=thortelem.createdevicehandler(vendorID, productID);
if(handle !=null) {
thortelem.claiminterface(handle);
Transfer transfer_in=LibUsb.allocTransfer(0);
//LibUsb.fillBulkTransfer(transfer_in,handle,OUT_ENDPOINT,in_buffer,10);
Random r = new Random();
char ch = (char)(r.nextInt(26) + 'a');
byte [] data = new byte[]{(byte)ch};
thortelem.initiatehandlerthread(handle, data,filename);
}
}
else
{
thortelem.deinitialiselibusb(handle);
//Activate_ATE_Actor.getThread("thor").interrupt();
}
my intiate thread handler is
public void initiatehandlerthread(DeviceHandle handle, byte[] data,String filename) {
// TODO Auto-generated method stub
try {
EventHandlingThread thread = new EventHandlingThread();
thread.start();
// Claim the ADB interface
claiminterface(handle);
Transfer transfer_in=LibUsb.allocTransfer();
TransferCallback cb_in = new TransferCallback()
{
@Override
public void processTransfer(Transfer transfer)
{
if(!exit) {
//System.out.println("called");
LibUsb.submitTransfer(transfer_in);
int length = transfer_in.actualLength() ;
//System.out.println("received"+length);
ByteBuffer out=transfer_in.buffer();
File f=new File(filename);
if(!f.exists())
{
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileChannel fc;
try (// FileChannel wChannel = new FileOutputStream(file, append).getChannel();
FileOutputStream fout = new FileOutputStream(filename,true)) {
for(int i=0;i<length;i++) {
fout.write(out.get(i));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
}
}
else {
System.out.println("exit is true");
//thread.abort();
}
}
};
// ByteBuffer in_buffer = BufferUtils.allocateByteBuffer(LEN_IN_BUFFER);
ByteBuffer in_buffer = BufferUtils.allocateByteBuffer(LEN_IN_BUFFER).order(
ByteOrder.LITTLE_ENDIAN);
LibUsb.fillBulkTransfer(transfer_in, handle, IN_ENDPOINT, in_buffer,cb_in ,0, TIMEOUT);
write(handle, data, cb_in);
int r=LibUsb.submitTransfer(transfer_in);
logger.info("Fetch telemetry data in progress");
ClientHandler2.senddata("Fetch telemetry data in progress");
}catch(Exception e)
{
e.printStackTrace();
}
// This callback is called after the ADB answer header has been
// received and reads the ADB answer body
}
My deinitializelibusb called when client send s stop is public void deinitialiselibusb(DeviceHandle handle) {
int result = LibUsb.releaseInterface(handle, INTERFACE);
logger.info("Deinitialised device");
ClientHandler2.senddata("Deinitialised device");
// Close the device
LibUsb.close(handle);
// Deinitialize the libusb context
try {
LibUsb.exit(context);
}catch(IllegalStateException e)
{
//context not initialised for stop
}
}
all initialise, create handler ,claim interface , writing, reading works fine for the first time i receive start after when i get stop and again start . initialise, create handler ,claim interface , writing works fine but the callaback mether cb_in is not called for the second time resulting no data appended. I have some static data declared below are them . Not sure whether any of these are the issue.
static Context context = new Context();
static short vendorID= 0x1cbe;
static short productID=0x0003;
private static byte INTERFACE = 0;
private static byte IN_ENDPOINT = LibUsb.ENDPOINT_IN |1;
private static byte OUT_ENDPOINT = LibUsb.ENDPOINT_OUT|2;
private static int TIMEOUT = 50000;
static int LEN_IN_BUFFER= 1024*8;
static ByteBuffer in_buffer = ByteBuffer.allocate(LEN_IN_BUFFER);
static volatile boolean exit = false;
static DeviceHandle handle;