package com.ifpdos.factorytest.mic.hid;

import com.ifpdos.mcusdk.McuCommandList;
import java.io.File;
import java.io.FileInputStream;

/* JADX INFO: loaded from: classes.dex */
public class DeviceUtils {
    private static final int DEV_VENDOR_ID = 8183;
    private static final String HIDRAW_PATH = "/sys/class/hidraw";
    private static final byte[] COMM_DEV_REPORT_DESC = {6, -1, -1, 9, 0, McuCommandList.MAIN_MCU_REMOTE_STATUS, 1, -123, 1, 21, 0, 37, -1, 25, 0, 41, -1, -106, -1, 1, 117, 8, -127, 2, 25, 0, 41, -1, -106, -1, 1, -111, 2, -64};
    private static final byte[] CTRL_DEV_REPORT_DESC = {6, -1, -1, 9, 0, McuCommandList.MAIN_MCU_REMOTE_STATUS, 1, -123, 1, 21, 0, 37, -1, 25, 0, 41, -1, -106, -1, 1, 117, 8, -127, 2, 25, 0, 41, -1, -106, -1, 7, -111, 2, -64};
    private static final int[][] OTHER_SPECIAL_VID_PIDS = {new int[]{9588, 1795}, new int[]{9588, 1796}};

    /* JADX WARN: Code restructure failed: missing block: B:42:0x0094, code lost:
    
        r2.close();
        r12.close();
        r1.close();
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:77:0x00de A[Catch: IOException -> 0x00da, TryCatch #1 {IOException -> 0x00da, blocks: (B:73:0x00d6, B:77:0x00de, B:79:0x00e3), top: B:84:0x00d6 }] */
    /* JADX WARN: Removed duplicated region for block: B:79:0x00e3 A[Catch: IOException -> 0x00da, TRY_LEAVE, TryCatch #1 {IOException -> 0x00da, blocks: (B:73:0x00d6, B:77:0x00de, B:79:0x00e3), top: B:84:0x00d6 }] */
    /* JADX WARN: Removed duplicated region for block: B:84:0x00d6 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r12v0, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r12v13 */
    /* JADX WARN: Type inference failed for: r12v14, types: [java.io.InputStreamReader, java.io.Reader] */
    /* JADX WARN: Type inference failed for: r12v2 */
    /* JADX WARN: Type inference failed for: r12v4 */
    /* JADX WARN: Type inference failed for: r12v5, types: [java.io.InputStreamReader] */
    /* JADX WARN: Type inference failed for: r12v7, types: [java.io.InputStreamReader] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private static java.lang.String isTargetDev(java.lang.String r12) throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 235
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ifpdos.factorytest.mic.hid.DeviceUtils.isTargetDev(java.lang.String):java.lang.String");
    }

    private static byte[] readReportDescriptor(String str) {
        byte[] bArr = new byte[256];
        try {
            if (new FileInputStream(str + "/device/report_descriptor").read(bArr) <= 0) {
                return null;
            }
            return bArr;
        } catch (Exception unused) {
            return null;
        }
    }

    private static boolean isTheSameDesc(byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr.length < bArr2.length) {
            return false;
        }
        for (int i = 0; i < bArr2.length; i++) {
            if (bArr2[i] != bArr[i]) {
                return false;
            }
        }
        return true;
    }

    public static String getDeviceCommPath() {
        File[] fileArrListFiles = new File(HIDRAW_PATH).listFiles();
        if (fileArrListFiles != null && fileArrListFiles.length > 0) {
            for (int i = 0; i < fileArrListFiles.length; i++) {
                if (fileArrListFiles[i].isDirectory() && isTargetDev(fileArrListFiles[i].getPath()) != null) {
                    byte[] reportDescriptor = readReportDescriptor(fileArrListFiles[i].getPath());
                    if (reportDescriptor != null) {
                        if (isTheSameDesc(reportDescriptor, COMM_DEV_REPORT_DESC)) {
                            return "/dev/" + fileArrListFiles[i].getName();
                        }
                    } else {
                        return "/dev/" + fileArrListFiles[i].getName();
                    }
                }
            }
        }
        return null;
    }

    public static String getDeviceCtrlPath() {
        File[] fileArrListFiles = new File(HIDRAW_PATH).listFiles();
        if (fileArrListFiles != null && fileArrListFiles.length > 0) {
            int i = 0;
            for (int i2 = 0; i2 < fileArrListFiles.length; i2++) {
                if (fileArrListFiles[i2].isDirectory() && isTargetDev(fileArrListFiles[i2].getPath()) != null) {
                    i++;
                    byte[] reportDescriptor = readReportDescriptor(fileArrListFiles[i2].getPath());
                    if (reportDescriptor != null) {
                        if (isTheSameDesc(reportDescriptor, CTRL_DEV_REPORT_DESC)) {
                            return "/dev/" + fileArrListFiles[i2].getName();
                        }
                    } else if (i == 2) {
                        return "/dev/" + fileArrListFiles[i2].getName();
                    }
                }
            }
        }
        return null;
    }
}
