// Path: src/main/java/commandwrappers/CommandBlockWrapper.java
// public class CommandBlockWrapper extends CommandWrapper
// {
//     private int dCBWSignature;
//     private int dCBWTag;
//     private int dCBWDataLength;
//     private byte bmCBWFlags;
//     private byte bCBWLUN;
//     private byte bCBWCBLength;
//     private byte[] commandBlock;
// 
// 
//     public CommandBlockWrapper(int dCBWSignature, int dCBWTag, int dCBWDataLength,
//                                byte bmCBWFlags, byte bCBWLUN, byte bCBWCBLength)
//     {
//         this.dCBWSignature = dCBWSignature;
//         this.dCBWTag = dCBWTag;
//         this.dCBWDataLength = dCBWDataLength;
//         this.bmCBWFlags = bmCBWFlags;
//         this.bCBWLUN = bCBWLUN;
//         this.bCBWCBLength = bCBWCBLength;
//     }
// 
//     public CommandBlockWrapper( int dCBWDataLength,
//                                 byte bmCBWFlags, byte bCBWLUN, byte bCBWCBLength)
//     {
//         this.dCBWSignature = CommandWrapper.CBW_SIGNATURE;
//         this.dCBWTag = generateTag();
//         this.dCBWDataLength = dCBWDataLength;
//         this.bmCBWFlags = bmCBWFlags;
//         this.bCBWLUN = bCBWLUN;
//         this.bCBWCBLength = bCBWCBLength;
//     }
// 
//     public void setCommandBlock(byte[] commandBlock)
//     {
//         this.commandBlock = commandBlock;
//     }
// 
//     public int getdCBWDataLength()
//     {
//         return dCBWDataLength;
//     }
// 
//     public int generateTag()
//     {
//         Random random = new Random();
//         return random.nextInt();
//     }
// 
//     @Override
//     public byte[] getCWBuffer()
//     {
//         ByteBuffer buffer = ByteBuffer.allocate(CBW_SIZE);
//         buffer.putInt(EndianessUtil.swapEndianess(dCBWSignature));
//         buffer.putInt(dCBWTag);
//         buffer.putInt(EndianessUtil.swapEndianess(dCBWDataLength));
//         buffer.put(bmCBWFlags);
//         buffer.put(bCBWLUN);
//         buffer.put(bCBWCBLength);
//         buffer.put(commandBlock);
//         return buffer.array();
//     }
// }
// 
// Path: src/main/java/commandwrappers/CommandStatusWrapper.java
// public class CommandStatusWrapper extends CommandWrapper
// {
//     private int dCSWSignature;
//     private int dCSWTag;
//     private int dCSWDataResidue;
//     private byte bCSWStatus;
// 
//     public CommandStatusWrapper(int dCSWSignature, int dCSWTag, int dCSWDataResidue,
//                                 byte bCSWStatus)
//     {
//         this.dCSWSignature = dCSWSignature;
//         this.dCSWTag = dCSWTag;
//         this.dCSWDataResidue = dCSWDataResidue;
//         this.bCSWStatus = bCSWStatus;
//     }
// 
//     public static CommandStatusWrapper getCWStatus(byte[] buffer)
//     {
//         int signature = convertToInt(Arrays.copyOfRange(buffer, 0, 4));
//         int tag = convertToInt(Arrays.copyOfRange(buffer, 4, 8));
//         int residue = convertToInt(Arrays.copyOfRange(buffer, 8, 12));
//         byte status = buffer[12];
//         return new CommandStatusWrapper(signature, tag, residue, status);
//     }
// 
//     private static int convertToInt(byte[] buffer)
//     {
//         int result;
//         result = buffer[0] & 0xff;
//         result = result + ((buffer[1] & 0xff) << 8);
//         result = result + ((buffer[2] & 0xff) << 16);
//         return result + ((buffer[3] & 0xff) << 24);
//     }
// 
//     @Override
//     public byte[] getCWBuffer()
//     {
//         ByteBuffer buffer = ByteBuffer.allocate(CBS_SIZE);
//         buffer.putInt(dCSWSignature);
//         buffer.putInt(dCSWTag);
//         buffer.putInt(dCSWDataResidue);
//         buffer.put(bCSWStatus);
//         return buffer.array();
//     }
// 
//     public int getdCSWSignature()
//     {
//         return dCSWSignature;
//     }
// 
//     public void setdCSWSignature(int dCSWSignature)
//     {
//         this.dCSWSignature = dCSWSignature;
//     }
// 
//     public int getdCSWTag()
//     {
//         return dCSWTag;
//     }
// 
//     public void setdCSWTag(int dCSWTag)
//     {
//         this.dCSWTag = dCSWTag;
//     }
// 
//     public int getdCSWDataResidue()
//     {
//         return dCSWDataResidue;
//     }
// 
//     public void setdCSWDataResidue(int dCSWDataResidue)
//     {
//         this.dCSWDataResidue = dCSWDataResidue;
//     }
// 
//     public byte getbCSWStatus()
//     {
//         return bCSWStatus;
//     }
// 
//     public void setbCSWStatus(byte bCSWStatus)
//     {
//         this.bCSWStatus = bCSWStatus;
//     }
// }


import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.util.Log;
import java.util.concurrent.atomic.AtomicBoolean;
import commandwrappers.CommandBlockWrapper;
import commandwrappers.CommandStatusWrapper;
package com.felhr.usbmassstorageforandroid.bulkonly;




/**
 * Created by Felipe Herranz(felhr85@gmail.com) on 9/12/14.
 */
public class BulkOnlyCommunicator
{
    private BulkOnlyStatusInterface statusCallback;

    private UsbFacade usbFacade;

    private AtomicBoolean flagDataIN;
    private AtomicBoolean flagDataOUT;

    public BulkOnlyCommunicator(UsbDevice mDevice, UsbDeviceConnection mConnection)
    {
        this.usbFacade = new UsbFacade(mDevice, mConnection);
        this.flagDataIN = new AtomicBoolean(false);
        this.flagDataOUT = new AtomicBoolean(false);
    }

    public boolean startBulkOnly(BulkOnlyStatusInterface statusCallback)
    {
        this.statusCallback = statusCallback;
        usbFacade.setCallback(mCallback);
        return usbFacade.openDevice();
    }

// Path: src/main/java/commandwrappers/CommandBlockWrapper.java
// public class CommandBlockWrapper extends CommandWrapper
// {
//     private int dCBWSignature;
//     private int dCBWTag;
//     private int dCBWDataLength;
//     private byte bmCBWFlags;
//     private byte bCBWLUN;
//     private byte bCBWCBLength;
//     private byte[] commandBlock;
// 
// 
//     public CommandBlockWrapper(int dCBWSignature, int dCBWTag, int dCBWDataLength,
//                                byte bmCBWFlags, byte bCBWLUN, byte bCBWCBLength)
//     {
//         this.dCBWSignature = dCBWSignature;
//         this.dCBWTag = dCBWTag;
//         this.dCBWDataLength = dCBWDataLength;
//         this.bmCBWFlags = bmCBWFlags;
//         this.bCBWLUN = bCBWLUN;
//         this.bCBWCBLength = bCBWCBLength;
//     }
// 
//     public CommandBlockWrapper( int dCBWDataLength,
//                                 byte bmCBWFlags, byte bCBWLUN, byte bCBWCBLength)
//     {
//         this.dCBWSignature = CommandWrapper.CBW_SIGNATURE;
//         this.dCBWTag = generateTag();
//         this.dCBWDataLength = dCBWDataLength;
//         this.bmCBWFlags = bmCBWFlags;
//         this.bCBWLUN = bCBWLUN;
//         this.bCBWCBLength = bCBWCBLength;
//     }
// 
//     public void setCommandBlock(byte[] commandBlock)
//     {
//         this.commandBlock = commandBlock;
//     }
// 
//     public int getdCBWDataLength()
//     {
//         return dCBWDataLength;
//     }
// 
//     public int generateTag()
//     {
//         Random random = new Random();
//         return random.nextInt();
//     }
// 
//     @Override
//     public byte[] getCWBuffer()
//     {
//         ByteBuffer buffer = ByteBuffer.allocate(CBW_SIZE);
//         buffer.putInt(EndianessUtil.swapEndianess(dCBWSignature));
//         buffer.putInt(dCBWTag);
//         buffer.putInt(EndianessUtil.swapEndianess(dCBWDataLength));
//         buffer.put(bmCBWFlags);
//         buffer.put(bCBWLUN);
//         buffer.put(bCBWCBLength);
//         buffer.put(commandBlock);
//         return buffer.array();
//     }
// }
// 
// Path: src/main/java/commandwrappers/CommandStatusWrapper.java
// public class CommandStatusWrapper extends CommandWrapper
// {
//     private int dCSWSignature;
//     private int dCSWTag;
//     private int dCSWDataResidue;
//     private byte bCSWStatus;
// 
//     public CommandStatusWrapper(int dCSWSignature, int dCSWTag, int dCSWDataResidue,
//                                 byte bCSWStatus)
//     {
//         this.dCSWSignature = dCSWSignature;
//         this.dCSWTag = dCSWTag;
//         this.dCSWDataResidue = dCSWDataResidue;
//         this.bCSWStatus = bCSWStatus;
//     }
// 
//     public static CommandStatusWrapper getCWStatus(byte[] buffer)
//     {
//         int signature = convertToInt(Arrays.copyOfRange(buffer, 0, 4));
//         int tag = convertToInt(Arrays.copyOfRange(buffer, 4, 8));
//         int residue = convertToInt(Arrays.copyOfRange(buffer, 8, 12));
//         byte status = buffer[12];
//         return new CommandStatusWrapper(signature, tag, residue, status);
//     }
// 
//     private static int convertToInt(byte[] buffer)
//     {
//         int result;
//         result = buffer[0] & 0xff;
//         result = result + ((buffer[1] & 0xff) << 8);
//         result = result + ((buffer[2] & 0xff) << 16);
//         return result + ((buffer[3] & 0xff) << 24);
//     }
// 
//     @Override
//     public byte[] getCWBuffer()
//     {
//         ByteBuffer buffer = ByteBuffer.allocate(CBS_SIZE);
//         buffer.putInt(dCSWSignature);
//         buffer.putInt(dCSWTag);
//         buffer.putInt(dCSWDataResidue);
//         buffer.put(bCSWStatus);
//         return buffer.array();
//     }
// 
//     public int getdCSWSignature()
//     {
//         return dCSWSignature;
//     }
// 
//     public void setdCSWSignature(int dCSWSignature)
//     {
//         this.dCSWSignature = dCSWSignature;
//     }
// 
//     public int getdCSWTag()
//     {
//         return dCSWTag;
//     }
// 
//     public void setdCSWTag(int dCSWTag)
//     {
//         this.dCSWTag = dCSWTag;
//     }
// 
//     public int getdCSWDataResidue()
//     {
//         return dCSWDataResidue;
//     }
// 
//     public void setdCSWDataResidue(int dCSWDataResidue)
//     {
//         this.dCSWDataResidue = dCSWDataResidue;
//     }
// 
//     public byte getbCSWStatus()
//     {
//         return bCSWStatus;
//     }
// 
//     public void setbCSWStatus(byte bCSWStatus)
//     {
//         this.bCSWStatus = bCSWStatus;
//     }
// }


// Path: src/main/java/com/felhr/usbmassstorageforandroid/bulkonly/BulkOnlyCommunicator.java
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.util.Log;
import java.util.concurrent.atomic.AtomicBoolean;
import commandwrappers.CommandBlockWrapper;
import commandwrappers.CommandStatusWrapper;

package com.felhr.usbmassstorageforandroid.bulkonly;




/**
 * Created by Felipe Herranz(felhr85@gmail.com) on 9/12/14.
 */
public class BulkOnlyCommunicator
{
    private BulkOnlyStatusInterface statusCallback;

    private UsbFacade usbFacade;

    private AtomicBoolean flagDataIN;
    private AtomicBoolean flagDataOUT;

    public BulkOnlyCommunicator(UsbDevice mDevice, UsbDeviceConnection mConnection)
    {
        this.usbFacade = new UsbFacade(mDevice, mConnection);
        this.flagDataIN = new AtomicBoolean(false);
        this.flagDataOUT = new AtomicBoolean(false);
    }

    public boolean startBulkOnly(BulkOnlyStatusInterface statusCallback)
    {
        this.statusCallback = statusCallback;
        usbFacade.setCallback(mCallback);
        return usbFacade.openDevice();
    }

    public void sendCbw(CommandBlockWrapper cbw, byte[] data)