package com.cvte.dss.ups.mc31.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 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\u0006\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\b-\u0010.J#\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\u0019\u0010\r\u001a\u0004\u0018\u00010\u00062\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b\r\u0010\u000eJ\u0019\u0010\u000f\u001a\u0004\u0018\u00010\u00062\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b\u000f\u0010\u000eJ\u001f\u0010\u0013\u001a\u0004\u0018\u00010\u00122\u0006\u0010\u0011\u001a\u00020\u00102\u0006\u0010\n\u001a\u00020\t¢\u0006\u0004\b\u0013\u0010\u0014J!\u0010\u0017\u001a\u00020\u00162\b\u0010\u0015\u001a\u0004\u0018\u00010\u00122\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b\u0017\u0010\u0018J3\u0010\u001e\u001a\u00020\u00042\b\u0010\u0019\u001a\u0004\u0018\u00010\u00122\b\u0010\u001a\u001a\u0004\u0018\u00010\u00062\b\u0010\u001c\u001a\u0004\u0018\u00010\u001b2\u0006\u0010\u001d\u001a\u00020\u0004¢\u0006\u0004\b\u001e\u0010\u001fJ3\u0010\"\u001a\u0004\u0018\u00010\u001b2\b\u0010\u0019\u001a\u0004\u0018\u00010\u00122\b\u0010 \u001a\u0004\u0018\u00010\u00062\u0006\u0010\u001d\u001a\u00020\u00042\u0006\u0010!\u001a\u00020\u0004¢\u0006\u0004\b\"\u0010#J!\u0010$\u001a\u00020\u00162\b\u0010\u0015\u001a\u0004\u0018\u00010\u00122\b\u0010\u0003\u001a\u0004\u0018\u00010\u0002¢\u0006\u0004\b$\u0010\u0018J\u0017\u0010&\u001a\u00020%2\b\u0010\u0015\u001a\u0004\u0018\u00010\u0012¢\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+\u0010,¨\u0006/"}, d2 = {"Lcom/cvte/dss/ups/mc31/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;", "getOutEndPoint", "(Landroid/hardware/usb/UsbInterface;)Landroid/hardware/usb/UsbEndpoint;", "getInEndPoint", "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) {
        if (usbInterface == null) {
            return null;
        }
        int endpointCount = usbInterface.getEndpointCount();
        for (int i2 = 0; i2 < endpointCount; i2++) {
            UsbEndpoint endpoint = usbInterface.getEndpoint(i2);
            o.b(endpoint, "usbEndpoint");
            if (endpoint.getType() == 3 && endpoint.getDirection() == endPointType) {
                return endpoint;
            }
        }
        return null;
    }

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

    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 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];
            if (usbConnection.bulkTransfer(epIn, bArr, length, timeOut) > 0) {
                return bArr;
            }
            strF = "Receiving data error";
        }
        Log.d(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.d(TAG, "send data error");
                }
                return iBulkTransfer;
            }
            strF = a.f(epOut, a.F("sendData direction is "), ", not USB_DIR_OUT");
        }
        Log.d(TAG, strF);
        return -1;
    }

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