package com.cvte.dss.ups.camera.utils;

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 d.b.a.a.a;
import g.h0.c.l;

/* JADX INFO: compiled from: UsbUtils.kt */
/* JADX INFO: loaded from: classes.dex */
public final class UsbUtils {
    public static final UsbUtils INSTANCE = new UsbUtils();
    private static final String TAG = "UsbUtils";
    private static final int TIME_OUT = 3000;

    /* JADX INFO: compiled from: UsbUtils.kt */
    public static final class UsbHidData {
        private final UsbEndpoint inPoint;
        private final UsbEndpoint outPoint;
        private final UsbInterface usbInterface;

        public UsbHidData(UsbInterface usbInterface, UsbEndpoint usbEndpoint, UsbEndpoint usbEndpoint2) {
            this.usbInterface = usbInterface;
            this.inPoint = usbEndpoint;
            this.outPoint = usbEndpoint2;
        }

        public static /* synthetic */ UsbHidData copy$default(UsbHidData usbHidData, UsbInterface usbInterface, UsbEndpoint usbEndpoint, UsbEndpoint usbEndpoint2, int i2, Object obj) {
            if ((i2 & 1) != 0) {
                usbInterface = usbHidData.usbInterface;
            }
            if ((i2 & 2) != 0) {
                usbEndpoint = usbHidData.inPoint;
            }
            if ((i2 & 4) != 0) {
                usbEndpoint2 = usbHidData.outPoint;
            }
            return usbHidData.copy(usbInterface, usbEndpoint, usbEndpoint2);
        }

        public final UsbInterface component1() {
            return this.usbInterface;
        }

        public final UsbEndpoint component2() {
            return this.inPoint;
        }

        public final UsbEndpoint component3() {
            return this.outPoint;
        }

        public final UsbHidData copy(UsbInterface usbInterface, UsbEndpoint usbEndpoint, UsbEndpoint usbEndpoint2) {
            return new UsbHidData(usbInterface, usbEndpoint, usbEndpoint2);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof UsbHidData)) {
                return false;
            }
            UsbHidData usbHidData = (UsbHidData) obj;
            return l.a(this.usbInterface, usbHidData.usbInterface) && l.a(this.inPoint, usbHidData.inPoint) && l.a(this.outPoint, usbHidData.outPoint);
        }

        public final UsbEndpoint getInPoint() {
            return this.inPoint;
        }

        public final UsbEndpoint getOutPoint() {
            return this.outPoint;
        }

        public final UsbInterface getUsbInterface() {
            return this.usbInterface;
        }

        public int hashCode() {
            UsbInterface usbInterface = this.usbInterface;
            int iHashCode = (usbInterface != null ? usbInterface.hashCode() : 0) * 31;
            UsbEndpoint usbEndpoint = this.inPoint;
            int iHashCode2 = (iHashCode + (usbEndpoint != null ? usbEndpoint.hashCode() : 0)) * 31;
            UsbEndpoint usbEndpoint2 = this.outPoint;
            return iHashCode2 + (usbEndpoint2 != null ? usbEndpoint2.hashCode() : 0);
        }

        public String toString() {
            StringBuilder sbB = a.B("UsbHidData(usbInterface=");
            sbB.append(this.usbInterface);
            sbB.append(", inPoint=");
            sbB.append(this.inPoint);
            sbB.append(", outPoint=");
            sbB.append(this.outPoint);
            sbB.append(")");
            return sbB.toString();
        }
    }

    private UsbUtils() {
    }

    private final UsbEndpoint getEndPoint(UsbInterface usbInterface, int i2) {
        if (usbInterface == null) {
            return null;
        }
        int endpointCount = usbInterface.getEndpointCount();
        for (int i3 = 0; i3 < endpointCount; i3++) {
            UsbEndpoint endpoint = usbInterface.getEndpoint(i3);
            l.b(endpoint, "usbEndpoint");
            if (endpoint.getType() == 3 && endpoint.getDirection() == i2) {
                return endpoint;
            }
        }
        return null;
    }

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

    public final UsbHidData getInterface(UsbDevice usbDevice) {
        if (usbDevice == null) {
            return null;
        }
        int interfaceCount = usbDevice.getInterfaceCount();
        for (int i2 = 0; i2 < interfaceCount; i2++) {
            UsbInterface usbInterface = usbDevice.getInterface(i2);
            l.b(usbInterface, "usbDevice.getInterface(i)");
            if (usbInterface.getInterfaceClass() == 3) {
                UsbEndpoint inEndPoint = getInEndPoint(usbInterface);
                UsbEndpoint outEndPoint = getOutEndPoint(usbInterface);
                if (inEndPoint != null && outEndPoint != null) {
                    return new UsbHidData(usbInterface, inEndPoint, outEndPoint);
                }
            }
        }
        return null;
    }

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

    public final boolean lockInterface(UsbDeviceConnection usbDeviceConnection, UsbInterface usbInterface) {
        if (usbDeviceConnection == null || usbInterface == null) {
            return false;
        }
        boolean zClaimInterface = usbDeviceConnection.claimInterface(usbInterface, true);
        a.P("claimInterface：", zClaimInterface, TAG);
        return zClaimInterface;
    }

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

    public final byte[] receiveData(UsbDeviceConnection usbDeviceConnection, UsbEndpoint usbEndpoint, int i2) {
        if (usbDeviceConnection == null) {
            Log.d(TAG, "receiveData usbConnection is null");
            return null;
        }
        if (usbEndpoint == null) {
            Log.d(TAG, "receiveData epIn is null");
            return null;
        }
        if (usbEndpoint.getDirection() != 128) {
            StringBuilder sbB = a.B("receiveData direction is ");
            sbB.append(usbEndpoint.getDirection());
            sbB.append(", not USB_DIR_IN");
            Log.d(TAG, sbB.toString());
            return null;
        }
        byte[] bArr = new byte[i2];
        int iBulkTransfer = usbDeviceConnection.bulkTransfer(usbEndpoint, bArr, i2, TIME_OUT);
        Log.d(TAG, "Receiving data length" + iBulkTransfer);
        if (iBulkTransfer <= 0) {
            Log.d(TAG, "Receiving data error");
            return null;
        }
        StringBuilder sbB2 = a.B("receiveData: ");
        sbB2.append(CommonUtils.INSTANCE.bytesToString(bArr));
        Log.d(TAG, sbB2.toString());
        return bArr;
    }

    public final int sendData(UsbDeviceConnection usbDeviceConnection, UsbEndpoint usbEndpoint, byte[] bArr, int i2) {
        if (usbDeviceConnection == null) {
            Log.d(TAG, "sendData usbConnection is null");
            return -1;
        }
        if (usbEndpoint == null) {
            Log.d(TAG, "sendData epOut is null");
            return -1;
        }
        if (usbEndpoint.getDirection() == 0) {
            int iBulkTransfer = usbDeviceConnection.bulkTransfer(usbEndpoint, bArr, i2, TIME_OUT);
            if (iBulkTransfer <= 0) {
                Log.d(TAG, "send data error");
            }
            return iBulkTransfer;
        }
        StringBuilder sbB = a.B("sendData direction is ");
        sbB.append(usbEndpoint.getDirection());
        sbB.append(", not USB_DIR_OUT");
        Log.d(TAG, sbB.toString());
        return -1;
    }

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