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

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import com.cvte.dss.ups.camera.zyzh.listener.UpgradeProgressListener;
import com.cvte.dss.ups.camera.zyzh.support.ICameraUpgrade;
import com.cvte.dss.ups.camera.zyzh.support.ShellUpgrade;
import com.cvte.dss.ups.camera.zyzh.support.VmanUpgrade;
import com.cvte.udi.utils.ByteUtils;
import com.cvte.udi.utils.CommonUtils;
import com.cvte.udi.utils.FileHelper;
import com.cvte.udi.utils.UsbUtils;
import com.cvte.udi.utils.ZipUtils;
import com.cvte.vman.instance.InvokeCmd;
import g.a.a.a.a;
import io.netty.handler.codec.dns.DnsRecord;
import java.io.File;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
public class CameraDevice {
    private static final byte CAMERA_MAIN_CODE = 32;
    private static final byte CAMERA_SUB_REQUEST_CODE_ENTER_UPGRADE = 8;
    private static final byte CAMERA_SUB_REQUEST_CODE_GET_MODULE = 10;
    private static final byte CAMERA_SUB_REQUEST_CODE_GET_OTA = 14;
    private static final byte CAMERA_SUB_REQUEST_CODE_GET_VERSION = 6;
    private static final byte CAMERA_SUB_RESPONSE_CODE_ENTER_UPGRADE = 9;
    private static final byte CAMERA_SUB_RESPONSE_CODE_GET_MODULE = 11;
    private static final byte CAMERA_SUB_RESPONSE_CODE_GET_OTA = 15;
    private static final byte CAMERA_SUB_RESPONSE_CODE_GET_VERSION = 7;
    private static final String CHARSET = "ISO8859-1";
    private static final int CHECK_STATUS_TIME_MS = 500;
    private static final int COMMAND_DATA_BEGIN_INDEX = 7;
    private static final int COMMAND_DATA_LENGTH_INDEX = 5;
    private static final int COMMAND_DATA_MAIN_INDEX = 3;
    private static final int COMMAND_DATA_SUB_INDEX = 4;
    private static final int COUNT_CHECK_FAST_BOOT = 40;
    private static final int COUNT_CHECK_VERSION = 120;
    private static final int COUNT_SEND_FAST_BOOT_COMMAND = 5;
    private static final int HID_BUF_LEN = 64;
    private static final String TAG = "CameraDevice";
    private static final String UNZIP_DIR = "cameraUpgradeUnzipTmp";
    public static final String UPGRADE_FILE_BOOT_IMG = "boot.img";
    public static final String UPGRADE_FILE_LK_BIN = "lk.bin";
    public static final String UPGRADE_FILE_SYSTEM_IMG = "system.img";
    public static final String VERSION_IN_FAST_BOOT = "0.0.0.0";
    private static final String ZIP_SUFFIX = ".zip";
    private static final ArrayList<CameraInfo> ZYZH_CAMERA_DEVICES = new ArrayList() { // from class: com.cvte.dss.ups.camera.zyzh.CameraDevice.1
        {
            add(new CameraInfo(9546, 3074));
            add(new CameraInfo(9546, 3075));
            add(new CameraInfo(9546, InvokeCmd.CMD_NETWORK_WOL_GET_ENABLE));
            add(new CameraInfo(9546, InvokeCmd.CMD_NETWORK_SET_MCU_DHCP_IP));
            add(new CameraInfo(2996, 3073));
        }
    };
    private final ICameraUpgrade mCameraSupport;
    private String mId = "";
    private UsbUtils.UsbDeviceWrapper mUsbDevice;

    public static class CameraInfo {
        public int pid;
        public int vid;

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

    public CameraDevice(Context context) {
        if (SystemPropertyUtil.isUseVmanUpgrade()) {
            this.mCameraSupport = new VmanUpgrade();
        } else {
            this.mCameraSupport = new ShellUpgrade(context);
        }
    }

    private boolean enterFastBoot() {
        if (isFastBoot()) {
            return true;
        }
        for (int i2 = 0; i2 < 5; i2++) {
            if (hasEnterFastBoot(sendUpgradeMessage())) {
                for (int i3 = 0; i3 < 40; i3++) {
                    if (checkFastBoot()) {
                        return true;
                    }
                    CommonUtils.safeSleep(500L);
                }
            } else {
                CommonUtils.safeSleep(500L);
            }
        }
        return false;
    }

    private UsbDevice findUsbDevice(Context context) {
        for (UsbDevice usbDevice : UsbUtils.getDevices(context)) {
            for (CameraInfo cameraInfo : ZYZH_CAMERA_DEVICES) {
                if (UsbUtils.isTargetDevice(usbDevice, cameraInfo.vid, cameraInfo.pid)) {
                    return usbDevice;
                }
            }
        }
        return null;
    }

    private byte[] getCameraCommand(byte b2) {
        byte[] bArr = new byte[64];
        bArr[0] = -86;
        bArr[1] = -69;
        bArr[2] = -52;
        bArr[3] = 32;
        bArr[4] = b2;
        bArr[63] = getCheckSum(bArr, 63);
        try {
            return this.mUsbDevice.bulkTransfer(bArr);
        } catch (Exception e2) {
            Log.e(TAG, "getCameraCommand", e2);
            return null;
        }
    }

    private String getCameraMessage(byte b2, byte b3) {
        byte[] cameraCommand;
        UsbUtils.UsbDeviceWrapper usbDeviceWrapper = this.mUsbDevice;
        if (usbDeviceWrapper == null || !usbDeviceWrapper.isConnect() || (cameraCommand = getCameraCommand(b2)) == 0 || cameraCommand.length < 6 || cameraCommand[3] != ' ' || cameraCommand[4] != b3) {
            return null;
        }
        int i2 = cameraCommand[5];
        byte[] bArr = new byte[i2];
        System.arraycopy(cameraCommand, 7, bArr, 0, i2);
        return ByteUtils.bytesToAscii(CHARSET, bArr, 0, i2);
    }

    private byte getCheckSum(byte[] bArr, int i2) {
        int i3 = 0;
        for (int i4 = 0; i4 < i2; i4++) {
            i3 += bArr[i4] & 255;
        }
        return (byte) ((i3 % 256) & DnsRecord.CLASS_ANY);
    }

    private boolean hasEnterFastBoot(byte[] bArr) {
        return bArr != null && bArr.length >= 5 && bArr[3] == 32 && bArr[4] == 9;
    }

    private boolean isFastBoot() {
        for (int i2 = 0; i2 < 10; i2++) {
            if (checkFastBoot()) {
                return true;
            }
            CommonUtils.safeSleep(500L);
        }
        return false;
    }

    private byte[] sendUpgradeMessage() {
        if (this.mUsbDevice.isConnect()) {
            return getCameraCommand((byte) 8);
        }
        return null;
    }

    private boolean upgrade(Context context, String str, String str2, UpgradeProgressListener upgradeProgressListener) {
        String str3;
        upgradeProgressListener.process(0.1f);
        File file = new File(str, UPGRADE_FILE_LK_BIN);
        File file2 = new File(str, UPGRADE_FILE_BOOT_IMG);
        File file3 = new File(str, UPGRADE_FILE_SYSTEM_IMG);
        if (file.exists() && file2.exists() && file3.exists()) {
            if (enterFastBoot()) {
                upgradeProgressListener.process(0.2f);
                if (!this.mCameraSupport.upgrade(context, str, str2, upgradeProgressListener)) {
                    return false;
                }
                upgradeProgressListener.process(0.9f);
                for (int i2 = 0; i2 < 120; i2++) {
                    refreshUsbDevice(context);
                    if (!this.mUsbDevice.isConnect()) {
                        this.mUsbDevice.connect(context);
                    }
                    String cameraMessage = getCameraMessage((byte) 6, (byte) 7);
                    if (cameraMessage != null && !TextUtils.isEmpty(cameraMessage)) {
                        Log.d(Plugin.TAG, "upgrade success");
                        upgradeProgressListener.process(1.0f);
                        return true;
                    }
                    CommonUtils.safeSleep(500L);
                }
                return false;
            }
            str3 = "enterFastBoot failed";
        } else {
            str3 = "lkBin or bootImg or systemImg not exist";
        }
        Log.d(Plugin.TAG, str3);
        return false;
    }

    public boolean checkFastBoot() {
        return this.mCameraSupport.checkFastBoot();
    }

    public boolean connect(Context context) {
        UsbUtils.UsbDeviceWrapper usbDeviceWrapper = this.mUsbDevice;
        if (usbDeviceWrapper == null) {
            return false;
        }
        try {
            usbDeviceWrapper.connect(context);
            return true;
        } catch (Exception e2) {
            Log.e(TAG, "connect error", e2);
            return true;
        }
    }

    public boolean disconnect() {
        UsbUtils.UsbDeviceWrapper usbDeviceWrapper = this.mUsbDevice;
        if (usbDeviceWrapper == null) {
            return false;
        }
        usbDeviceWrapper.disconnect();
        return true;
    }

    public String getId() {
        return this.mId;
    }

    public String getModule() {
        String cameraMessage = getCameraMessage((byte) 10, (byte) 11);
        return (TextUtils.isEmpty(cameraMessage) || checkFastBoot()) ? "zyzh" : cameraMessage;
    }

    public String getOTAKey() {
        String cameraMessage = getCameraMessage((byte) 14, (byte) 15);
        return (TextUtils.isEmpty(cameraMessage) && checkFastBoot()) ? this.mCameraSupport.getOtaKeyOnFastBoot() : cameraMessage;
    }

    public String getVersion() {
        String cameraMessage = getCameraMessage((byte) 6, (byte) 7);
        return (cameraMessage == null && checkFastBoot()) ? VERSION_IN_FAST_BOOT : cameraMessage;
    }

    public boolean refreshUsbDevice(Context context) {
        UsbDevice usbDeviceFindUsbDevice = findUsbDevice(context);
        if (usbDeviceFindUsbDevice == null) {
            return false;
        }
        this.mUsbDevice = new UsbUtils.UsbDeviceWrapper(usbDeviceFindUsbDevice);
        this.mId = CommonUtil.pidVidDecodeId(usbDeviceFindUsbDevice.getProductId(), usbDeviceFindUsbDevice.getVendorId());
        return true;
    }

    public boolean upgrade(Context context, File file, UpgradeProgressListener upgradeProgressListener, String str) {
        if (!file.exists()) {
            StringBuilder sbF = a.F("upgrade: ");
            sbF.append(file.getAbsolutePath());
            sbF.append(" not exists");
            Log.i(TAG, sbF.toString());
            return false;
        }
        if (file.isDirectory()) {
            return upgrade(context, file.getAbsolutePath(), str, upgradeProgressListener);
        }
        if (file.getName().endsWith(ZIP_SUFFIX)) {
            File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), UNZIP_DIR);
            if (!file2.exists() && !file2.mkdirs()) {
                StringBuilder sbF2 = a.F("upgrade unzipDir path = ");
                sbF2.append(file2.getAbsolutePath());
                sbF2.append(" not exists");
                Log.d(TAG, sbF2.toString());
                return false;
            }
            if (ZipUtils.unzip(file, file2)) {
                boolean zUpgrade = upgrade(context, file2, upgradeProgressListener, str);
                Log.d(TAG, "upgrade result：" + zUpgrade + " start delete zipDir" + file2.getAbsolutePath());
                a.k0("delete zipDir result : ", FileHelper.deleteFolder(file2.getAbsolutePath()), TAG);
                return zUpgrade;
            }
            Log.d(TAG, "upgrade unzip file fail");
        }
        return false;
    }
}
