package com.dss.ups.sp51a.dongle;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;
import g.a.a.a.a;
import java.util.Arrays;
import kotlin.Metadata;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000R\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\n\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0004\n\u0002\u0010\u0012\n\u0002\b\t\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0007\bÆ\u0002\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b1\u00102J#\u0010\u0007\u001a\u0004\u0018\u00010\u00062\b\u0010\u0003\u001a\u0004\u0018\u00010\u00022\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0007\u0010\bJ\u0019\u0010\u000b\u001a\u0004\u0018\u00010\u00022\b\u0010\n\u001a\u0004\u0018\u00010\t¢\u0006\u0004\b\u000b\u0010\fJ!\u0010\u000b\u001a\u0004\u0018\u00010\u00022\b\u0010\n\u001a\u0004\u0018\u00010\t2\u0006\u0010\r\u001a\u00020\u0004¢\u0006\u0004\b\u000b\u0010\u000eJ!\u0010\u0010\u001a\u0004\u0018\u00010\u00062\b\u0010\n\u001a\u0004\u0018\u00010\t2\u0006\u0010\u000f\u001a\u00020\u0004¢\u0006\u0004\b\u0010\u0010\u0011J!\u0010\u0012\u001a\u0004\u0018\u00010\u00062\b\u0010\n\u001a\u0004\u0018\u00010\t2\u0006\u0010\u000f\u001a\u00020\u0004¢\u0006\u0004\b\u0012\u0010\u0011J\u0019\u0010\u0010\u001a\u0004\u0018\u00010\u00062\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b\u0010\u0010\u0013J\u0019\u0010\u0012\u001a\u0004\u0018\u00010\u00062\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b\u0012\u0010\u0013J\u001f\u0010\u0017\u001a\u0004\u0018\u00010\u00162\u0006\u0010\u0015\u001a\u00020\u00142\u0006\u0010\n\u001a\u00020\t¢\u0006\u0004\b\u0017\u0010\u0018J!\u0010\u001b\u001a\u00020\u001a2\b\u0010\u0019\u001a\u0004\u0018\u00010\u00162\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b\u001b\u0010\u001cJ3\u0010\"\u001a\u00020\u00042\b\u0010\u001d\u001a\u0004\u0018\u00010\u00162\b\u0010\u001e\u001a\u0004\u0018\u00010\u00062\b\u0010 \u001a\u0004\u0018\u00010\u001f2\u0006\u0010!\u001a\u00020\u0004¢\u0006\u0004\b\"\u0010#J3\u0010&\u001a\u0004\u0018\u00010\u001f2\b\u0010\u001d\u001a\u0004\u0018\u00010\u00162\b\u0010$\u001a\u0004\u0018\u00010\u00062\u0006\u0010!\u001a\u00020\u00042\u0006\u0010%\u001a\u00020\u0004¢\u0006\u0004\b&\u0010'J!\u0010(\u001a\u00020\u001a2\b\u0010\u0019\u001a\u0004\u0018\u00010\u00162\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b(\u0010\u001cJ\u0017\u0010*\u001a\u00020)2\b\u0010\u0019\u001a\u0004\u0018\u00010\u0016¢\u0006\u0004\b*\u0010+R\u0016\u0010-\u001a\u00020,8\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b-\u0010.R\u0016\u0010/\u001a\u00020\u00048\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b/\u00100¨\u00063"}, d2 = {"Lcom/dss/ups/sp51a/dongle/HidUtils;", "", "Landroid/hardware/usb/UsbInterface;", "usbInterface", "", "endPointType", "Landroid/hardware/usb/UsbEndpoint;", "getEndPoint", "(Landroid/hardware/usb/UsbInterface;I)Landroid/hardware/usb/UsbEndpoint;", "Landroid/hardware/usb/UsbDevice;", "usbDevice", "getInterface", "(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbInterface;", "interfaceID", "(Landroid/hardware/usb/UsbDevice;I)Landroid/hardware/usb/UsbInterface;", "usbInterfaceId", "getOutEndPoint", "(Landroid/hardware/usb/UsbDevice;I)Landroid/hardware/usb/UsbEndpoint;", "getInEndPoint", "(Landroid/hardware/usb/UsbInterface;)Landroid/hardware/usb/UsbEndpoint;", "Landroid/hardware/usb/UsbManager;", "manager", "Landroid/hardware/usb/UsbDeviceConnection;", "openDevice", "(Landroid/hardware/usb/UsbManager;Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;", "usbDeviceConnection", "", "lockInterface", "(Landroid/hardware/usb/UsbDeviceConnection;Landroid/hardware/usb/UsbInterface;)Z", "usbConnection", "epOut", "", "buffer", "length", "sendData", "(Landroid/hardware/usb/UsbDeviceConnection;Landroid/hardware/usb/UsbEndpoint;[BI)I", "epIn", "timeOut", "receiveData", "(Landroid/hardware/usb/UsbDeviceConnection;Landroid/hardware/usb/UsbEndpoint;II)[B", "unLockInterface", "", "closeDevice", "(Landroid/hardware/usb/UsbDeviceConnection;)V", "", "TAG", "Ljava/lang/String;", "TIME_OUT", "I", "<init>", "()V", "lib_release"}, k = 1, mv = {1, 4, 0})
public final class HidUtils {
    public static final HidUtils INSTANCE = new HidUtils();
    private static final String TAG = "UsbUtils";
    private static final int TIME_OUT = 10000;

    private HidUtils() {
    }

    private final UsbEndpoint getEndPoint(UsbInterface usbInterface, int endPointType) {
        String str;
        if (usbInterface == null) {
            return null;
        }
        int endpointCount = usbInterface.getEndpointCount();
        for (int i2 = 0; i2 < endpointCount; i2++) {
            UsbEndpoint endpoint = usbInterface.getEndpoint(i2);
            StringBuilder sb = new StringBuilder();
            sb.append("endPoint: type=");
            o.b(endpoint, "usbEndpoint");
            sb.append(endpoint.getType());
            sb.append(", direction=");
            sb.append(endpoint.getDirection());
            Log.i(TAG, sb.toString());
            if (endPointType == 0 && endpoint.getDirection() == endPointType) {
                str = "get out point";
            } else if (endPointType == 128 && endpoint.getDirection() == endPointType) {
                str = "get in point";
            }
            Log.e(TAG, str);
            return endpoint;
        }
        return null;
    }

    public final void closeDevice(UsbDeviceConnection usbDeviceConnection) {
        if (usbDeviceConnection != null) {
            usbDeviceConnection.close();
        }
    }

    public final UsbEndpoint getInEndPoint(UsbDevice usbDevice, int usbInterfaceId) {
        if (usbDevice == null || usbInterfaceId > usbDevice.getInterfaceCount()) {
            return null;
        }
        UsbInterface usbInterface = usbDevice.getInterface(usbInterfaceId);
        o.b(usbInterface, "usbDevice.getInterface(usbInterfaceId)");
        Log.i(TAG, "getInEndPoint[" + usbInterfaceId + "],[" + usbDevice.getInterfaceCount() + "]: " + usbInterface.getInterfaceClass());
        return getEndPoint(usbInterface, 128);
    }

    public final UsbEndpoint getInEndPoint(UsbInterface usbInterface) {
        return getEndPoint(usbInterface, 128);
    }

    public final UsbInterface getInterface(UsbDevice usbDevice) {
        if (usbDevice == null) {
            return null;
        }
        int interfaceCount = usbDevice.getInterfaceCount();
        for (int i2 = 0; i2 < interfaceCount; i2++) {
            UsbInterface usbInterface = usbDevice.getInterface(i2);
            o.b(usbInterface, "usbDevice.getInterface(i)");
            if (usbInterface.getInterfaceClass() == 3) {
                return usbInterface;
            }
        }
        return null;
    }

    public final UsbInterface getInterface(UsbDevice usbDevice, int interfaceID) {
        if (usbDevice == null || interfaceID > usbDevice.getInterfaceCount()) {
            return null;
        }
        UsbInterface usbInterface = usbDevice.getInterface(interfaceID);
        o.b(usbInterface, "usbDevice.getInterface(interfaceID)");
        Log.i(TAG, "getInterface[" + interfaceID + "],[" + usbDevice.getInterfaceCount() + "]: " + usbInterface.getInterfaceClass());
        return usbInterface;
    }

    public final UsbEndpoint getOutEndPoint(UsbDevice usbDevice, int usbInterfaceId) {
        if (usbDevice == null || usbInterfaceId > usbDevice.getInterfaceCount()) {
            return null;
        }
        UsbInterface usbInterface = usbDevice.getInterface(usbInterfaceId);
        o.b(usbInterface, "usbDevice.getInterface(usbInterfaceId)");
        Log.i(TAG, "getOutEndPoint[" + usbInterfaceId + "],[" + usbDevice.getInterfaceCount() + "]: " + usbInterface.getInterfaceClass());
        return getEndPoint(usbInterface, 0);
    }

    public final UsbEndpoint getOutEndPoint(UsbInterface usbInterface) {
        return getEndPoint(usbInterface, 0);
    }

    public final boolean lockInterface(UsbDeviceConnection usbDeviceConnection, UsbInterface usbInterface) {
        return (usbDeviceConnection == null || usbInterface == null || !usbDeviceConnection.claimInterface(usbInterface, true)) ? false : true;
    }

    public final UsbDeviceConnection openDevice(UsbManager manager, UsbDevice usbDevice) {
        o.g(manager, "manager");
        o.g(usbDevice, "usbDevice");
        if (manager.hasPermission(usbDevice)) {
            return manager.openDevice(usbDevice);
        }
        return null;
    }

    public final byte[] receiveData(UsbDeviceConnection usbConnection, UsbEndpoint epIn, int length, int timeOut) {
        String strF;
        if (usbConnection == null) {
            strF = "receiveData usbConnection is null";
        } else if (epIn == null) {
            strF = "receiveData epIn is null";
        } else if (epIn.getDirection() != 128) {
            strF = a.f(epIn, a.F("receiveData direction is "), ", not USB_DIR_IN");
        } else {
            byte[] bArr = new byte[length];
            int iBulkTransfer = usbConnection.bulkTransfer(epIn, bArr, length, timeOut);
            StringBuilder sbG = a.G("receiveData n:", iBulkTransfer, ", ");
            String string = Arrays.toString(bArr);
            o.b(string, "java.util.Arrays.toString(this)");
            sbG.append(string);
            Log.i(TAG, sbG.toString());
            if (iBulkTransfer > 0) {
                return bArr;
            }
            strF = "Receiving data error";
        }
        Log.e(TAG, strF);
        return null;
    }

    public final int sendData(UsbDeviceConnection usbConnection, UsbEndpoint epOut, byte[] buffer, int length) {
        String strF;
        if (usbConnection == null) {
            strF = "sendData usbConnection is null";
        } else if (epOut == null) {
            strF = "sendData epOut is null";
        } else {
            if (epOut.getDirection() == 0) {
                int iBulkTransfer = usbConnection.bulkTransfer(epOut, buffer, length, 0);
                if (iBulkTransfer <= 0) {
                    Log.e(TAG, "send data error");
                }
                return iBulkTransfer;
            }
            strF = a.f(epOut, a.F("sendData direction is "), ", not USB_DIR_OUT");
        }
        Log.e(TAG, strF);
        return -1;
    }

    public final boolean unLockInterface(UsbDeviceConnection usbDeviceConnection, UsbInterface usbInterface) {
        return (usbDeviceConnection == null || usbInterface == null || !usbDeviceConnection.releaseInterface(usbInterface)) ? false : true;
    }
}
