package com.cvte.dss.ups.u3hub.hub;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;
import com.cvte.vman.instance.InvokeCmd;
import g.a.a.a.a;
import io.netty.handler.codec.dns.DnsRecord;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import kotlin.Metadata;
import kotlin.jvm.internal.o;
import no.nordicsemi.android.dfu.DfuBaseService;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000X\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u0012\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u0005\n\u0002\b\n\n\u0002\u0018\u0002\n\u0002\b\n\n\u0002\u0010\u000e\n\u0002\b\u001d\n\u0002\u0018\u0002\n\u0002\b\t\n\u0002\u0018\u0002\n\u0002\b\r\b&\u0018\u0000 Y2\u00020\u0001:\u0001YB\u0017\u0012\u0006\u0010J\u001a\u00020\r\u0012\u0006\u0010L\u001a\u00020\r¢\u0006\u0004\bW\u0010XJ/\u0010\t\u001a\u00020\b2\u0006\u0010\u0003\u001a\u00020\u00022\u0016\u0010\u0007\u001a\u0012\u0012\u0004\u0012\u00020\u00050\u0004j\b\u0012\u0004\u0012\u00020\u0005`\u0006H\u0002¢\u0006\u0004\b\t\u0010\nJ\u001f\u0010\u0010\u001a\u00020\u000f2\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u0010\u0010\u0011J\u001f\u0010\u0013\u001a\u00020\r2\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u0012\u001a\u00020\rH\u0002¢\u0006\u0004\b\u0013\u0010\u0014J\u001f\u0010\u0016\u001a\u00020\r2\u0006\u0010\u0015\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u0016\u0010\u0014J\u001f\u0010\u0018\u001a\u00020\b2\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u0017\u001a\u00020\rH\u0002¢\u0006\u0004\b\u0018\u0010\u0019J!\u0010\u001d\u001a\u00020\b2\b\u0010\u001b\u001a\u0004\u0018\u00010\u001a2\u0006\u0010\u001c\u001a\u00020\rH\u0002¢\u0006\u0004\b\u001d\u0010\u001eJ)\u0010!\u001a\u00020\b2\b\u0010\u001b\u001a\u0004\u0018\u00010\u001a2\u0006\u0010\u001f\u001a\u00020\u000b2\u0006\u0010 \u001a\u00020\u000bH\u0002¢\u0006\u0004\b!\u0010\"J\u0017\u0010#\u001a\u00020\b2\u0006\u0010\u0003\u001a\u00020\u0002H\u0004¢\u0006\u0004\b#\u0010$J\u0019\u0010&\u001a\u0004\u0018\u00010%2\u0006\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b&\u0010'J\u0015\u0010(\u001a\u00020\b2\u0006\u0010\u0003\u001a\u00020\u0002¢\u0006\u0004\b(\u0010$J\u001f\u0010*\u001a\u00020\b2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010)\u001a\u00020\u0005H\u0004¢\u0006\u0004\b*\u0010+J\u000f\u0010,\u001a\u00020\bH\u0004¢\u0006\u0004\b,\u0010-J+\u00100\u001a\u00020\b2\b\u0010.\u001a\u0004\u0018\u00010%2\b\u0010\u001b\u001a\u0004\u0018\u00010\u001a2\u0006\u0010/\u001a\u00020\u000fH\u0004¢\u0006\u0004\b0\u00101J+\u00103\u001a\u00020\b2\b\u00102\u001a\u0004\u0018\u00010%2\b\u0010\u001b\u001a\u0004\u0018\u00010\u001a2\u0006\u0010/\u001a\u00020\u000fH\u0004¢\u0006\u0004\b3\u00101J\u001f\u00105\u001a\u00020%2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u00104\u001a\u00020\u0005H&¢\u0006\u0004\b5\u00106J!\u00107\u001a\u00020\b2\b\u0010\u001b\u001a\u0004\u0018\u00010\u001a2\u0006\u0010/\u001a\u00020\u000fH&¢\u0006\u0004\b7\u00108J\u0017\u00109\u001a\u00020\r2\u0006\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b9\u0010:R\u0016\u0010;\u001a\u00020\b8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b;\u0010<R$\u0010=\u001a\u0004\u0018\u00010\u001a8\u0004@\u0004X\u0084\u000e¢\u0006\u0012\n\u0004\b=\u0010>\u001a\u0004\b?\u0010@\"\u0004\bA\u0010BR\"\u0010D\u001a\u00020C8\u0004@\u0004X\u0084\u000e¢\u0006\u0012\n\u0004\bD\u0010E\u001a\u0004\bF\u0010G\"\u0004\bH\u0010IR\u0016\u0010J\u001a\u00020\r8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\bJ\u0010KR\u0016\u0010L\u001a\u00020\r8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\bL\u0010KR\u0018\u0010N\u001a\u0004\u0018\u00010M8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bN\u0010OR&\u0010P\u001a\u0012\u0012\u0004\u0012\u00020\u00050\u0004j\b\u0012\u0004\u0012\u00020\u0005`\u00068\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\bP\u0010QR\u0016\u0010R\u001a\u00020\r8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bR\u0010KR\u0018\u0010S\u001a\u0004\u0018\u00010%8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bS\u0010TR\u0016\u0010U\u001a\u00020\b8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bU\u0010<R\u0016\u0010V\u001a\u00020\r8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bV\u0010K¨\u0006Z"}, d2 = {"Lcom/cvte/dss/ups/u3hub/hub/BaseHubUpgradeApi;", "Lcom/cvte/dss/ups/u3hub/hub/IHubUpgradeAction;", "Landroid/content/Context;", "context", "Ljava/util/ArrayList;", "Landroid/hardware/usb/UsbDevice;", "Lkotlin/collections/ArrayList;", "devices", "", "executeUpgradeThread", "(Landroid/content/Context;Ljava/util/ArrayList;)Z", "", "data", "", "len", "", "getCrc8", "([BI)B", "dwSize", "getCrc16", "([BI)I", "ptr", "directTableCrc32", "lengthWithCrc32", "verifyCrc2", "([BI)Z", "Landroid/hardware/usb/UsbDeviceConnection;", "connect", "hidIndex", "verifySpiFw1Crc32", "(Landroid/hardware/usb/UsbDeviceConnection;I)Z", "binData", "msgBuf", "doesHubFwMatchIC", "(Landroid/hardware/usb/UsbDeviceConnection;[B[B)Z", "realStartUpgrade", "(Landroid/content/Context;)Z", "", "getVersion", "(Landroid/content/Context;)Ljava/lang/String;", "enumerate", "Device", "openDevice", "(Landroid/content/Context;Landroid/hardware/usb/UsbDevice;)Z", "closeDevice", "()Z", "vl817Path", "spiType", "updateVT35xxFirmware", "(Ljava/lang/String;Landroid/hardware/usb/UsbDeviceConnection;B)Z", "vl103Path", "updateVL10xShareSPIFW", "device", "realGetVersion", "(Landroid/content/Context;Landroid/hardware/usb/UsbDevice;)Ljava/lang/String;", "dealUpgradeAction", "(Landroid/hardware/usb/UsbDeviceConnection;B)Z", "getDeviceNum", "(Landroid/content/Context;)I", "haveVliDevie", "Z", "mDeviceConnect", "Landroid/hardware/usb/UsbDeviceConnection;", "getMDeviceConnect", "()Landroid/hardware/usb/UsbDeviceConnection;", "setMDeviceConnect", "(Landroid/hardware/usb/UsbDeviceConnection;)V", "Lcom/cvte/dss/ups/u3hub/hub/VendorCommand;", "vendorCmd", "Lcom/cvte/dss/ups/u3hub/hub/VendorCommand;", "getVendorCmd", "()Lcom/cvte/dss/ups/u3hub/hub/VendorCommand;", "setVendorCmd", "(Lcom/cvte/dss/ups/u3hub/hub/VendorCommand;)V", "pid", "I", "vid", "Landroid/hardware/usb/UsbInterface;", "intf", "Landroid/hardware/usb/UsbInterface;", "mVLIDevices", "Ljava/util/ArrayList;", "errorIndex", "mCurrentVersion", "Ljava/lang/String;", "updateFlag", "mCurrentRetryCount", "<init>", "(II)V", "Companion", "lib_release"}, k = 1, mv = {1, 4, 0})
public abstract class BaseHubUpgradeApi implements IHubUpgradeAction {
    private static final int ERROR_DOWNLOAD_FIRMWARE1 = -10;
    private static final int ERROR_DOWNLOAD_HD1 = -5;
    private static final int ERROR_ERASE_FIRMWARE1 = -8;
    private static final int ERROR_ERASE_FIRMWARE2 = -9;
    private static final int ERROR_ERASE_HD1 = -4;
    private static final int ERROR_FIRMWARE_CODE = -3;
    private static final int ERROR_FIRMWARE_MISMATCH = -12;
    private static final int ERROR_FIRMWARE_SIZE = -2;
    private static final int ERROR_OPEN_FIRMWARE = -1;
    private static final int ERROR_PD_CRC16_FAIL = -22;
    private static final int ERROR_PD_ERASE_ZONE1 = -23;
    private static final int ERROR_PD_ERASE_ZONE2 = -24;
    private static final int ERROR_PD_OPEN_FIRMWARE = -21;
    private static final int ERROR_PD_WRITE_FIRMWARE1 = -25;
    private static final int ERROR_PD_WRITE_FIRMWARE2 = -26;
    private static final int ERROR_WRITE_FIRMWARE2 = -11;
    private static final int ERROR_WRITE_HD1 = -6;
    private static final int ERROR_WRITE_HD2 = -7;
    public static final String MSG_TAG = "HubActionTag";
    private static final int RETRY_COUNT = 5;
    private int errorIndex;
    private boolean haveVliDevie;
    private UsbInterface intf;
    private int mCurrentRetryCount;
    private String mCurrentVersion;
    private UsbDeviceConnection mDeviceConnect;
    private final int pid;
    private boolean updateFlag;
    private final int vid;
    private final ArrayList<UsbDevice> mVLIDevices = new ArrayList<>();
    private VendorCommand vendorCmd = new VendorCommand();

    public BaseHubUpgradeApi(int i2, int i3) {
        this.pid = i2;
        this.vid = i3;
    }

    private final int directTableCrc32(byte[] ptr, int len) {
        int[] iArr = new int[256];
        for (int i2 = 0; i2 < 256; i2++) {
            long j2 = i2 << 24;
            long j3 = 0;
            for (int i3 = 0; i3 < 8; i3++) {
                long j4 = (j2 ^ j3) & 2147483648L;
                j3 <<= 1;
                if (j4 != 0) {
                    j3 ^= (long) 79764919;
                }
                j2 <<= 1;
            }
            iArr[i2] = (int) (4294967295L & j3);
        }
        int i4 = 0;
        for (int i5 = 0; i5 < len; i5++) {
            i4 = iArr[(int) ((((long) (i4 >> 24)) & 255) ^ (255 & ((long) ptr[i5])))] ^ (i4 << 8);
        }
        return i4;
    }

    private final boolean doesHubFwMatchIC(UsbDeviceConnection connect, byte[] binData, byte[] msgBuf) {
        byte b2;
        byte b3;
        byte[] bArr = new byte[8];
        Log.d(MSG_TAG, "check_matching\n");
        byte b4 = (byte) 248;
        this.vendorCmd.Hub_Read_Register(connect, b4, (short) 143, bArr, false);
        byte b5 = bArr[0];
        this.vendorCmd.Hub_Read_Register(connect, b4, (short) 142, bArr, false);
        byte b6 = bArr[0];
        this.vendorCmd.Hub_Read_Register(connect, b4, (short) 140, bArr, false);
        byte b7 = bArr[0];
        this.vendorCmd.Hub_Read_Register(connect, (byte) 246, (short) 81, bArr, false);
        byte b8 = (byte) ((bArr[0] >> 1) & 7);
        byte b9 = binData[32798];
        byte b10 = binData[32799];
        byte b11 = binData[32801];
        byte b12 = (byte) (binData[((binData[32792] & 255) * 256) + 8192 + (binData[32793] & 255)] & 7);
        byte b13 = (byte) 5;
        byte b14 = (b12 == b13 || b12 == ((byte) 7)) ? b12 : (byte) (((b12 & 3) << 1) | ((b12 & 4) >> 2));
        msgBuf[0] = b5;
        msgBuf[1] = b6;
        msgBuf[2] = b7;
        msgBuf[3] = b8;
        msgBuf[4] = b9;
        msgBuf[5] = b10;
        msgBuf[6] = b11;
        msgBuf[7] = b14;
        byte b15 = (byte) 53;
        if (b5 == b15 && b6 == (b3 = (byte) 56)) {
            if (b9 == b15 && b10 == b3 && b7 == b11) {
                return true;
            }
            return binData[0] == b13 && binData[1] == b3 && b7 == ((byte) 176) && b11 != ((byte) 192) && ((b8 >> 1) & 1) == b12;
        }
        if (b5 != b15 || b6 != (b2 = (byte) 24)) {
            return b5 == b9 && b6 == b10 && b7 == b11;
        }
        if (b9 != b15 || b10 != b2) {
            return false;
        }
        byte b16 = (byte) 240;
        if (b7 != b16 && b11 != b16) {
            return true;
        }
        if (b7 != b16 || b11 == b16) {
            return (b7 == b16 || b11 != b16) && b8 == b14;
        }
        return false;
    }

    private final boolean executeUpgradeThread(Context context, ArrayList<UsbDevice> devices) {
        String string;
        Log.d(MSG_TAG, "ExcuteUpdateThread");
        this.updateFlag = true;
        boolean zDealUpgradeAction = false;
        for (UsbDevice usbDevice : devices) {
            StringBuilder sb = new StringBuilder();
            sb.append("Start upgrade ");
            o.b(usbDevice, "device");
            sb.append(usbDevice.getProductId());
            Log.d(MSG_TAG, sb.toString());
            byte[] bArr = new byte[4];
            if (openDevice(context, usbDevice)) {
                String str = String.format("VID: 0x%04X, PID: 0x%04X\n", Arrays.copyOf(new Object[]{Integer.valueOf(usbDevice.getVendorId()), Integer.valueOf(usbDevice.getProductId())}, 2));
                o.e(str, "java.lang.String.format(format, *args)");
                Log.d(MSG_TAG, str);
                this.vendorCmd.ReadFWVersion(this.mDeviceConnect, bArr, false);
                String str2 = String.format("Current FW Version: %02X%02X\n", Arrays.copyOf(new Object[]{Byte.valueOf(bArr[1]), Byte.valueOf(bArr[0])}, 2));
                o.e(str2, "java.lang.String.format(format, *args)");
                Log.d(MSG_TAG, str2);
                zDealUpgradeAction = dealUpgradeAction(this.mDeviceConnect, (byte) 0);
                if (zDealUpgradeAction) {
                    string = "FW Update Success";
                } else {
                    StringBuilder sbF = a.F("FW Update Fail ");
                    String str3 = String.format("=========    FAIL%02d    =========\n", Arrays.copyOf(new Object[]{Integer.valueOf(this.errorIndex)}, 1));
                    o.e(str3, "java.lang.String.format(format, *args)");
                    sbF.append(str3);
                    string = sbF.toString();
                }
            } else {
                string = "Open Device Fail !!";
            }
            Log.d(MSG_TAG, string);
            if (this.mDeviceConnect != null) {
                closeDevice();
            }
        }
        this.updateFlag = false;
        Log.d(MSG_TAG, "Please re-plug device.\n");
        return zDealUpgradeAction;
    }

    private final int getCrc16(byte[] data, int dwSize) {
        int i2 = 65535;
        for (int i3 = 0; i3 < dwSize; i3++) {
            i2 = (int) (((long) i2) ^ (((long) data[i3]) & 255));
            for (int i4 = 0; i4 <= 7; i4++) {
                int i5 = i2 & 1;
                i2 >>= 1;
                if (i5 == 1) {
                    i2 ^= 40961;
                }
            }
        }
        return (~i2) & 65535;
    }

    private final byte getCrc8(byte[] data, int len) {
        int i2 = 0;
        int i3 = 0;
        while (true) {
            if (len <= 0) {
                int i4 = i2 >> 8;
                String str = String.format("GetCrc8 = 0x%02X\n", Arrays.copyOf(new Object[]{Integer.valueOf(i4)}, 1));
                o.e(str, "java.lang.String.format(format, *args)");
                Log.d(MSG_TAG, str);
                return (byte) i4;
            }
            i2 ^= (data[i3] & 255) << 8;
            for (int i5 = 8; i5 > 0; i5--) {
                if ((i2 & DfuBaseService.ERROR_CONNECTION_STATE_MASK) == 32768) {
                    i2 ^= 33664;
                }
                i2 <<= 1;
            }
            len--;
            i3++;
        }
    }

    private final boolean verifyCrc2(byte[] data, int lengthWithCrc32) {
        int i2 = lengthWithCrc32 - 4;
        int iDirectTableCrc32 = directTableCrc32(data, i2);
        String str = String.format("GetCrc32 = 0x%08X\n", Arrays.copyOf(new Object[]{Integer.valueOf(iDirectTableCrc32)}, 1));
        o.e(str, "java.lang.String.format(format, *args)");
        Log.d(MSG_TAG, str);
        return data[i2] == ((byte) (iDirectTableCrc32 & DnsRecord.CLASS_ANY)) && data[i2 + 1] == ((byte) ((iDirectTableCrc32 >> 8) & DnsRecord.CLASS_ANY)) && data[i2 + 2] == ((byte) ((iDirectTableCrc32 >> 16) & DnsRecord.CLASS_ANY)) && data[i2 + 3] == ((byte) ((iDirectTableCrc32 >> 24) & DnsRecord.CLASS_ANY));
    }

    private final boolean verifySpiFw1Crc32(UsbDeviceConnection connect, int hidIndex) {
        String str;
        byte[] bArr = new byte[65536];
        byte b2 = (byte) 3;
        Arrays.fill(bArr, (byte) 0);
        int i2 = hidIndex * 32;
        int i3 = 0;
        while (true) {
            if (i3 >= 32) {
                int i4 = ((bArr[6] & 255) << 8) + (bArr[7] & 255);
                int i5 = 0;
                while (i5 < i4) {
                    int i6 = i4 - i5;
                    int i7 = i6 < 32 ? i6 : 32;
                    if (this.vendorCmd.SPI_Read_Data(connect, b2, 8192 + i5, i5, i7, bArr)) {
                        i5 += i7;
                    } else {
                        str = "Read_SPI_FW_Error";
                    }
                }
                return verifyCrc2(bArr, i4);
            }
            int i8 = 32 - i3;
            int i9 = i8 < 32 ? i8 : 32;
            if (!this.vendorCmd.SPI_Read_Data(connect, b2, i2, 0, i9, bArr)) {
                str = "Read_SPI_HD_Error";
                break;
            }
            i3 += i9;
        }
        Log.d(MSG_TAG, str);
        return false;
    }

    public final boolean closeDevice() {
        Log.d(MSG_TAG, "closeDevice");
        UsbDeviceConnection usbDeviceConnection = this.mDeviceConnect;
        if (usbDeviceConnection != null) {
            usbDeviceConnection.releaseInterface(this.intf);
        }
        UsbDeviceConnection usbDeviceConnection2 = this.mDeviceConnect;
        if (usbDeviceConnection2 != null) {
            usbDeviceConnection2.close();
        }
        this.mDeviceConnect = null;
        return true;
    }

    public abstract boolean dealUpgradeAction(UsbDeviceConnection connect, byte spiType);

    public final synchronized boolean enumerate(Context context) {
        boolean z2;
        int i2;
        o.g(context, "context");
        Log.d(MSG_TAG, "Enumerate");
        Object systemService = context.getSystemService("usb");
        if (!(systemService instanceof UsbManager)) {
            systemService = null;
        }
        UsbManager usbManager = (UsbManager) systemService;
        if (usbManager == null) {
            return false;
        }
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        Log.d(MSG_TAG, deviceList.size() + " USB devices found");
        this.mVLIDevices.clear();
        loop0: while (true) {
            z2 = false;
            for (UsbDevice usbDevice : deviceList.values()) {
                StringBuilder sb = new StringBuilder();
                sb.append("USB device found: vid = ");
                o.b(usbDevice, "device");
                sb.append(usbDevice.getVendorId());
                sb.append(", pid = ");
                sb.append(usbDevice.getProductId());
                Log.d(MSG_TAG, sb.toString());
                if (usbDevice.getVendorId() == this.vid && usbDevice.getProductId() == this.pid) {
                    String strRealGetVersion = realGetVersion(context, usbDevice);
                    if (strRealGetVersion.length() == 0) {
                        Log.d(MSG_TAG, "get single 817 version fail,continue");
                    } else {
                        this.mCurrentVersion = strRealGetVersion;
                        this.mVLIDevices.add(usbDevice);
                        Log.d(MSG_TAG, "Hub Found! Device Info: " + usbDevice);
                        if (usbManager.hasPermission(usbDevice)) {
                            Log.d(MSG_TAG, "permission allowed");
                            z2 = true;
                        }
                    }
                }
            }
            Log.d(MSG_TAG, "permission not allowed");
        }
        Log.d(MSG_TAG, "Found " + this.mVLIDevices.size() + " Device");
        if (!z2 && (i2 = this.mCurrentRetryCount) < 5) {
            this.mCurrentRetryCount = i2 + 1;
            Log.d(MSG_TAG, "Is " + this.mCurrentRetryCount + " Retry");
            enumerate(context);
        }
        return z2;
    }

    @Override // com.cvte.dss.ups.u3hub.hub.IHubUpgradeAction
    public int getDeviceNum(Context context) {
        o.g(context, "context");
        enumerate(context);
        return this.mVLIDevices.size();
    }

    public final UsbDeviceConnection getMDeviceConnect() {
        return this.mDeviceConnect;
    }

    public final VendorCommand getVendorCmd() {
        return this.vendorCmd;
    }

    @Override // com.cvte.dss.ups.u3hub.hub.IHubUpgradeAction
    public String getVersion(Context context) {
        o.g(context, "context");
        this.mCurrentRetryCount = 0;
        enumerate(context);
        return this.mCurrentVersion;
    }

    public final boolean openDevice(Context context, UsbDevice Device) {
        o.g(context, "context");
        o.g(Device, "Device");
        Log.d(MSG_TAG, "openDevice");
        Object systemService = context.getSystemService("usb");
        if (!(systemService instanceof UsbManager)) {
            systemService = null;
        }
        UsbManager usbManager = (UsbManager) systemService;
        if (usbManager == null) {
            return false;
        }
        UsbDeviceConnection usbDeviceConnectionOpenDevice = usbManager.openDevice(Device);
        Log.d(MSG_TAG, "openDevice Info: " + Device + ", connection:" + usbDeviceConnectionOpenDevice);
        if (usbDeviceConnectionOpenDevice == null) {
            Log.d(MSG_TAG, "openDevice Fail");
            return false;
        }
        this.mDeviceConnect = usbDeviceConnectionOpenDevice;
        StringBuilder sbF = a.F("connection:");
        sbF.append(this.mDeviceConnect);
        Log.d(MSG_TAG, sbF.toString());
        UsbInterface usbInterface = Device.getInterface(0);
        this.intf = usbInterface;
        usbDeviceConnectionOpenDevice.claimInterface(usbInterface, true);
        return true;
    }

    public abstract String realGetVersion(Context context, UsbDevice device);

    public final boolean realStartUpgrade(Context context) {
        o.g(context, "context");
        Log.d(MSG_TAG, "startUpgrade");
        this.mCurrentRetryCount = 0;
        boolean zEnumerate = enumerate(context);
        this.haveVliDevie = zEnumerate;
        if (zEnumerate) {
            return executeUpgradeThread(context, this.mVLIDevices);
        }
        Log.d(MSG_TAG, "No VLI device");
        return false;
    }

    public final void setMDeviceConnect(UsbDeviceConnection usbDeviceConnection) {
        this.mDeviceConnect = usbDeviceConnection;
    }

    public final void setVendorCmd(VendorCommand vendorCommand) {
        o.g(vendorCommand, "<set-?>");
        this.vendorCmd = vendorCommand;
    }

    public final boolean updateVL10xShareSPIFW(String vl103Path, UsbDeviceConnection connect, byte spiType) {
        int iAvailable;
        int i2;
        boolean z2;
        int i3;
        FileInputStream fileInputStream;
        byte[] bArr = new byte[65536];
        Log.d(MSG_TAG, "UpdateVL10x_Start");
        Arrays.fill(bArr, (byte) 0);
        try {
            fileInputStream = new FileInputStream(vl103Path);
            iAvailable = fileInputStream.available();
        } catch (IOException e2) {
            e = e2;
            iAvailable = 0;
        }
        try {
            String str = String.format("TotalSize = 0x%X\n", Arrays.copyOf(new Object[]{Integer.valueOf(iAvailable)}, 1));
            o.e(str, "java.lang.String.format(format, *args)");
            Log.d(MSG_TAG, str);
            if (iAvailable > 65536) {
                iAvailable = 65536;
            }
            fileInputStream.read(bArr, 0, iAvailable);
            fileInputStream.close();
            i2 = iAvailable;
            z2 = true;
        } catch (IOException e3) {
            e = e3;
            Log.d(MSG_TAG, "Open_FW_File_Fail", e);
            i2 = iAvailable;
            z2 = false;
        }
        if (z2) {
            int i4 = i2 - 2;
            int crc16 = getCrc16(bArr, i4);
            if (bArr[i4] == ((byte) (crc16 & DnsRecord.CLASS_ANY)) && bArr[i2 - 1] == ((byte) (crc16 >> 8))) {
                int i5 = 131072;
                while (true) {
                    if (i5 >= i2 + 131072) {
                        try {
                            Log.d(MSG_TAG, "Erase firmware1 finish,wait 500ms");
                            Thread.sleep(500L);
                        } catch (InterruptedException e4) {
                            e4.printStackTrace();
                        }
                        Log.d(MSG_TAG, "- Write FW Data to Zone 1 (FW1)..");
                        int i6 = i2;
                        if (this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 131072, 0, i2, bArr, true)) {
                            int i7 = i6 > 32768 ? 196608 : 163840;
                            int i8 = i7;
                            while (true) {
                                if (i8 >= i7 + i6) {
                                    try {
                                        Log.d(MSG_TAG, "Erase firmware2 finish,wait 500ms");
                                        Thread.sleep(500L);
                                    } catch (InterruptedException e5) {
                                        e5.printStackTrace();
                                    }
                                    Log.d(MSG_TAG, "- Write FW Data to Zone 2 (FW2)..");
                                    if (this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, i7, 0, i6, bArr, true)) {
                                        Log.d(MSG_TAG, "Update VL10x firmware OK\n");
                                        this.errorIndex = 0;
                                        return true;
                                    }
                                    Log.d(MSG_TAG, "Write_FW2_Zone_Error");
                                    i3 = ERROR_PD_WRITE_FIRMWARE2;
                                } else {
                                    if (!this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, i8, true)) {
                                        Log.d(MSG_TAG, "Erase_FW2_Zone_Error");
                                        i3 = ERROR_PD_ERASE_ZONE2;
                                        break;
                                    }
                                    i8 += 4096;
                                }
                            }
                        } else {
                            Log.d(MSG_TAG, "Write_FW1_Zone_Error");
                            i3 = ERROR_PD_WRITE_FIRMWARE1;
                        }
                    } else {
                        if (!this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, i5, true)) {
                            Log.d(MSG_TAG, "Erase_FW1_Zone_Error");
                            i3 = ERROR_PD_ERASE_ZONE1;
                            break;
                        }
                        i5 += 4096;
                    }
                }
            } else {
                Log.d(MSG_TAG, "Bin FileError.\n");
                i3 = ERROR_PD_CRC16_FAIL;
            }
        } else {
            Log.d(MSG_TAG, "Open firmware fail.\n");
            i3 = ERROR_PD_OPEN_FIRMWARE;
        }
        this.errorIndex = i3;
        return false;
    }

    public final boolean updateVT35xxFirmware(String vl817Path, UsbDeviceConnection connect, byte spiType) {
        byte b2;
        boolean z2;
        char c2;
        int i2;
        byte crc8;
        boolean z3;
        byte b3;
        byte[] bArr;
        byte b4;
        int i3;
        int i4;
        int i5;
        int i6;
        FileInputStream fileInputStream;
        int iAvailable;
        int i7;
        int i8;
        byte[] bArr2 = new byte[65536];
        byte[] bArr3 = new byte[65536];
        byte[] bArr4 = new byte[256];
        byte[] bArr5 = new byte[32];
        byte[] bArr6 = new byte[32];
        byte b5 = (byte) 3;
        Log.d(MSG_TAG, "VLI U3 hub update start...\n");
        byte b6 = (byte) 0;
        Arrays.fill(bArr2, b6);
        Arrays.fill(bArr3, b6);
        Arrays.fill(bArr4, b6);
        Arrays.fill(bArr5, b6);
        Arrays.fill(bArr6, b6);
        Log.d(MSG_TAG, "Open_FW_File");
        try {
            fileInputStream = new FileInputStream(vl817Path);
            iAvailable = fileInputStream.available();
            b2 = b6;
        } catch (IOException e2) {
            e = e2;
            b2 = b6;
        }
        try {
            String str = String.format("TotalSize = 0x%X\n", Arrays.copyOf(new Object[]{Integer.valueOf(iAvailable)}, 1));
            o.e(str, "java.lang.String.format(format, *args)");
            Log.d(MSG_TAG, str);
            if (iAvailable > 65536) {
                i8 = 0;
                i7 = 65536;
            } else {
                i7 = iAvailable;
                i8 = 0;
            }
            fileInputStream.read(bArr2, i8, i7);
            fileInputStream.close();
            z2 = true;
        } catch (IOException e3) {
            e = e3;
            Log.d(MSG_TAG, "Open_FW_File_Fail", e);
            z2 = false;
        }
        if (!z2) {
            Log.d(MSG_TAG, "Open firmware fail.\n");
            this.errorIndex = -1;
            return false;
        }
        for (int i9 = 0; i9 < 32; i9++) {
            bArr5[i9] = bArr2[i9];
        }
        if (getCrc8(bArr5, 31) != bArr5[31]) {
            Log.d(MSG_TAG, "Firmware header error\n");
            this.errorIndex = -3;
            return false;
        }
        int i10 = ((bArr5[6] & 255) << 8) + (bArr5[7] & 255) + 12;
        if (i10 > 57344) {
            Log.d(MSG_TAG, "Firmware size error\n");
            this.errorIndex = -2;
            return false;
        }
        for (int i11 = 0; i11 < i10; i11++) {
            bArr3[i11] = bArr2[8192 + i11];
        }
        if (!verifyCrc2(bArr3, i10 + ERROR_FIRMWARE_MISMATCH)) {
            Log.d(MSG_TAG, "Firmware code error\n");
            this.errorIndex = -3;
            return false;
        }
        StringBuilder sb = new StringBuilder();
        String str2 = String.format("Original_Bin_FW_Start_Addr_0x%X\n", Arrays.copyOf(new Object[]{8192}, 1));
        o.e(str2, "java.lang.String.format(format, *args)");
        sb.append(str2);
        String str3 = String.format("Original_Bin_FW_Size_(+12)_0x%X\n", Arrays.copyOf(new Object[]{Integer.valueOf(i10)}, 1));
        o.e(str3, "java.lang.String.format(format, *args)");
        sb.append(str3);
        Log.d(MSG_TAG, sb.toString());
        if (!doesHubFwMatchIC(connect, bArr2, bArr4)) {
            StringBuilder sbF = a.F("Firmware match error\n ");
            String str4 = String.format("Reg %02X%02X, %02X, %02X \n", Arrays.copyOf(new Object[]{Byte.valueOf(bArr4[0]), Byte.valueOf(bArr4[1]), Byte.valueOf(bArr4[2]), Byte.valueOf(bArr4[3])}, 4));
            o.e(str4, "java.lang.String.format(format, *args)");
            sbF.append(str4);
            sbF.append(' ');
            String str5 = String.format("Bin %02X%02X, %02X, %02X \n", Arrays.copyOf(new Object[]{Byte.valueOf(bArr4[4]), Byte.valueOf(bArr4[5]), Byte.valueOf(bArr4[6]), Byte.valueOf(bArr4[7])}, 4));
            o.e(str5, "java.lang.String.format(format, *args)");
            sbF.append(str5);
            Log.d(MSG_TAG, sbF.toString());
            this.errorIndex = ERROR_FIRMWARE_MISMATCH;
            return false;
        }
        byte b7 = b2;
        this.vendorCmd.SPI_Read_Data(connect, b5, 0, 0, 32, bArr6);
        byte crc82 = getCrc8(bArr6, 31);
        if (crc82 != bArr6[31]) {
            Log.d(MSG_TAG, "Check C0 Header\n");
            i2 = 192;
            Arrays.fill(bArr6, b7);
            this.vendorCmd.SPI_Read_Data(connect, b5, InvokeCmd.CMD_RS232_CTRL_START, 0, 32, bArr6);
            c2 = 31;
            crc8 = getCrc8(bArr6, 31);
        } else {
            c2 = 31;
            i2 = 0;
            crc8 = crc82;
        }
        if (crc8 == bArr6[c2]) {
            Log.d(MSG_TAG, "Chk_SPI_FW1_CRC");
            if (verifySpiFw1Crc32(connect, i2)) {
                Log.d(MSG_TAG, "FW1_Exist");
                z3 = true;
            } else {
                Log.d(MSG_TAG, "FW1 not exist\n");
                z3 = false;
            }
        } else {
            z3 = false;
        }
        boolean z4 = z3;
        if (z4) {
            Log.d(MSG_TAG, "Update firmware erasing...\nWrite_FW2_Process_Erasing..");
            byte b8 = (byte) DnsRecord.CLASS_ANY;
            bArr6[29] = b8;
            bArr6[31] = getCrc8(bArr6, 31);
            if (i2 == 0) {
                this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, 4096, true);
                b4 = b8;
                if (this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, InvokeCmd.CMD_RS232_CTRL_START, 0, 32, bArr6, true)) {
                    this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, 0, true);
                    if (this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 0, 0, 32, bArr6, true)) {
                        i4 = 0;
                        i3 = -6;
                    } else {
                        Log.d(MSG_TAG, "Temporary hd1 error.\n");
                        i6 = -6;
                    }
                } else {
                    Log.d(MSG_TAG, "Temporary hd2 error.\n");
                    i6 = -7;
                }
                this.errorIndex = i6;
                return false;
            }
            b4 = b8;
            i3 = -6;
            i4 = 0;
            if (i2 != 0) {
                this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, i4, true);
                b3 = b7;
                i5 = i3;
                if (!this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 0, 0, 32, bArr6, true)) {
                    Log.d(MSG_TAG, "Temporary hd3 error.\n");
                    this.errorIndex = i5;
                    return false;
                }
                this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, 4096, true);
                if (!this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, InvokeCmd.CMD_RS232_CTRL_START, 0, 32, bArr6, true)) {
                    Log.d(MSG_TAG, "Temporary hd4 error.\n");
                    this.errorIndex = -7;
                    return false;
                }
            } else {
                b3 = b7;
                i5 = i3;
            }
            int i12 = 65536;
            for (int i13 = 65536; i12 < i10 + i13; i13 = 65536) {
                if (!this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, i12, true)) {
                    Log.d(MSG_TAG, "Erase_FW2_Zone_Error\n Erase firmware2 error.\n");
                    this.errorIndex = ERROR_ERASE_FIRMWARE2;
                    return false;
                }
                i12 += 4096;
            }
            try {
                Log.d(MSG_TAG, "Erase firmware2 finish,wait 500ms");
                Thread.sleep(500L);
            } catch (InterruptedException e4) {
                e4.printStackTrace();
            }
            Log.d(MSG_TAG, "- Write FW Data to Update Block (FW2)..");
            if (!this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 65536, 0, i10, bArr3, true)) {
                Log.d(MSG_TAG, "Write firmware2 error.\n");
                this.errorIndex = ERROR_WRITE_FIRMWARE2;
                return false;
            }
            Log.d(MSG_TAG, "- Write FW Header to Update Block (HD2)");
            bArr5[4] = b3;
            bArr5[5] = b3;
            bArr5[12] = (byte) 1;
            bArr5[28] = b3;
            bArr5[29] = b4;
            bArr5[31] = getCrc8(bArr5, 31);
            bArr = bArr5;
            if (!this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 4096, 0, 32, bArr5, true)) {
                Log.d(MSG_TAG, "Write header2 error.");
                this.errorIndex = -7;
                return false;
            }
            Log.d(MSG_TAG, "- Update Default Block Header (HD1)");
            if (!this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, 0, true)) {
                Log.d(MSG_TAG, "Erase_HD1_Error");
                this.errorIndex = -4;
                return false;
            }
            bArr6[29] = (byte) 128;
            bArr6[31] = getCrc8(bArr6, 31);
            if (!this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 0, 0, 32, bArr6, true)) {
                Log.d(MSG_TAG, "Write_HD1_Error");
                this.errorIndex = i5;
                return false;
            }
        } else {
            b3 = b7;
            bArr = bArr5;
        }
        if (!z4) {
            Log.d(MSG_TAG, "Write_FW1_Process_Erasing...");
            for (int i14 = 0; i14 < 65536; i14 += 4096) {
                if (!this.vendorCmd.SPI_SeRdCmp_Exe(connect, spiType, i14, true)) {
                    Log.d(MSG_TAG, "Erase_FW1_Error");
                    this.errorIndex = ERROR_ERASE_FIRMWARE1;
                    return false;
                }
            }
            try {
                Log.d(MSG_TAG, "Erase firmware1 finish,wait 500ms");
                Thread.sleep(500L);
            } catch (InterruptedException e5) {
                e5.printStackTrace();
            }
            Log.d(MSG_TAG, "Write_FW1_Process_Programming..");
            Log.d(MSG_TAG, "- Write FW Data (FW1)");
            if (!this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 8192, 0, i10, bArr3, true)) {
                Log.d(MSG_TAG, "Write_FW1_Error");
                this.errorIndex = ERROR_DOWNLOAD_FIRMWARE1;
                return false;
            }
            Log.d(MSG_TAG, "- Write FW Header (HD1)");
            byte[] bArr7 = bArr;
            bArr7[4] = (byte) 32;
            bArr7[5] = b3;
            byte b9 = (byte) DnsRecord.CLASS_ANY;
            bArr7[28] = b9;
            bArr7[29] = b9;
            bArr7[31] = getCrc8(bArr7, 31);
            if (!this.vendorCmd.SPI_WrRdCmp_Exe(connect, spiType, 0, 0, 32, bArr7, true)) {
                Log.d(MSG_TAG, "Write_HD1_Error");
                this.errorIndex = -5;
                return false;
            }
        }
        Log.d(MSG_TAG, "Update firmware OK");
        this.errorIndex = 0;
        return true;
    }
}
