package com.cvte.dss.ups.mc31.dongle;

import android.content.Context;
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 com.cvte.dss.ups.plugin.IUpsPlugin;
import com.cvte.vman.instance.InvokeCmd;
import g.a.a.a.a;
import g.h.a.i;
import io.netty.handler.codec.dns.DnsRecord;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import kotlin.Metadata;
import kotlin.TypeCastException;
import kotlin.io.f;
import kotlin.jvm.internal.o;
import kotlin.text.Charsets;
import kotlin.text.j;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000h\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0010\u0012\n\u0000\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0002\b\n\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0018\u0002\n\u0002\b\f\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0005\bÆ\u0002\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\bF\u0010\u000bJ\u001f\u0010\u0007\u001a\u00020\u00062\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0007\u0010\bJ\u000f\u0010\n\u001a\u00020\tH\u0002¢\u0006\u0004\b\n\u0010\u000bJ!\u0010\u0010\u001a\u0004\u0018\u00010\f2\u0006\u0010\r\u001a\u00020\f2\u0006\u0010\u000f\u001a\u00020\u000eH\u0002¢\u0006\u0004\b\u0010\u0010\u0011J\u001f\u0010\u0016\u001a\u00020\u00062\u0006\u0010\u0013\u001a\u00020\u00122\u0006\u0010\u0015\u001a\u00020\u0014H\u0002¢\u0006\u0004\b\u0016\u0010\u0017J!\u0010\u0019\u001a\u0004\u0018\u00010\f2\u0006\u0010\u0018\u001a\u00020\f2\u0006\u0010\u000f\u001a\u00020\u000eH\u0002¢\u0006\u0004\b\u0019\u0010\u0011J+\u0010\u001d\u001a\u0004\u0018\u00010\u00122\b\u0010\u001a\u001a\u0004\u0018\u00010\f2\u0006\u0010\u001b\u001a\u00020\u000e2\u0006\u0010\u001c\u001a\u00020\u000eH\u0002¢\u0006\u0004\b\u001d\u0010\u001eJ\u0017\u0010!\u001a\u0004\u0018\u00010\u00042\u0006\u0010 \u001a\u00020\u001f¢\u0006\u0004\b!\u0010\"J\u0015\u0010#\u001a\u00020\u00062\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b#\u0010$J\u0017\u0010%\u001a\u0004\u0018\u00010\u00122\u0006\u0010 \u001a\u00020\u001f¢\u0006\u0004\b%\u0010&J\u0017\u0010'\u001a\u0004\u0018\u00010\u00122\u0006\u0010 \u001a\u00020\u001f¢\u0006\u0004\b'\u0010&J\u0017\u0010)\u001a\u0004\u0018\u00010(2\u0006\u0010\u0013\u001a\u00020\u0012¢\u0006\u0004\b)\u0010*J\u001d\u0010,\u001a\u00020\u00062\u0006\u0010 \u001a\u00020\u001f2\u0006\u0010+\u001a\u00020\u0012¢\u0006\u0004\b,\u0010-J\u0015\u0010.\u001a\u00020\u000e2\u0006\u0010 \u001a\u00020\u001f¢\u0006\u0004\b.\u0010/J%\u00100\u001a\u00020\u00062\u0006\u0010 \u001a\u00020\u001f2\u0006\u0010\u0013\u001a\u00020\u00122\u0006\u0010\u0015\u001a\u00020\u0014¢\u0006\u0004\b0\u00101J\u001d\u00103\u001a\u00020\u00062\u0006\u0010 \u001a\u00020\u001f2\u0006\u00102\u001a\u00020\u0006¢\u0006\u0004\b3\u00104R\u0018\u00106\u001a\u0004\u0018\u0001058\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b6\u00107R\u0016\u00108\u001a\u00020\u00128\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b8\u00109R\u0016\u0010:\u001a\u00020\u00128\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b:\u00109R\u0016\u0010;\u001a\u00020\u000e8\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b;\u0010<R\u0016\u0010=\u001a\u00020\u000e8\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b=\u0010<R\u0018\u0010?\u001a\u0004\u0018\u00010>8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b?\u0010@R\u0016\u0010A\u001a\u00020\u000e8\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\bA\u0010<R\u0018\u0010C\u001a\u0004\u0018\u00010B8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bC\u0010DR\u0018\u0010E\u001a\u0004\u0018\u00010B8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bE\u0010D¨\u0006G"}, d2 = {"Lcom/cvte/dss/ups/mc31/dongle/MicDongleHid;", "", "Landroid/hardware/usb/UsbManager;", "manager", "Landroid/hardware/usb/UsbDevice;", "device", "", "connect", "(Landroid/hardware/usb/UsbManager;Landroid/hardware/usb/UsbDevice;)Z", "", "disconnect", "()V", "", "data", "", "timeOut", "sendHidCommand", "([BI)[B", "", "filePath", "Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;", "listener", "sendFirmwarePackage", "(Ljava/lang/String;Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "buffer", "bulkTransfer", "bytes", "offset", "dataLen", "bytesToAscii", "([BII)Ljava/lang/String;", "Landroid/content/Context;", "context", "findDevice", "(Landroid/content/Context;)Landroid/hardware/usb/UsbDevice;", "isMicDongleUsb", "(Landroid/hardware/usb/UsbDevice;)Z", "getVersion", "(Landroid/content/Context;)Ljava/lang/String;", "getOtaKey", "Lcom/cvte/dss/ups/mc31/dongle/FileInfo;", "CalculateFileSizeAndSum", "(Ljava/lang/String;)Lcom/cvte/dss/ups/mc31/dongle/FileInfo;", "path", "upgradeFirmwareStart", "(Landroid/content/Context;Ljava/lang/String;)Z", "getUpgradeProcess", "(Landroid/content/Context;)I", "uploadFile", "(Landroid/content/Context;Ljava/lang/String;Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "success", "upgradeFirmwareEnd", "(Landroid/content/Context;Z)Z", "Landroid/hardware/usb/UsbDeviceConnection;", "connection", "Landroid/hardware/usb/UsbDeviceConnection;", "TAG", "Ljava/lang/String;", "CHARSET", "HID_BUF_LEN", "I", "DONGLE_VID", "Landroid/hardware/usb/UsbInterface;", "usbInterface", "Landroid/hardware/usb/UsbInterface;", "DONGLE_PID", "Landroid/hardware/usb/UsbEndpoint;", "inPoint", "Landroid/hardware/usb/UsbEndpoint;", "outPoint", "<init>", "lib_release"}, k = 1, mv = {1, 4, 0})
public final class MicDongleHid {
    private static final String CHARSET = "ISO8859-1";
    private static final int DONGLE_PID = 1552;
    private static final int DONGLE_VID = 8183;
    private static final int HID_BUF_LEN = 64;
    public static final MicDongleHid INSTANCE = new MicDongleHid();
    private static final String TAG = "MicDongleHid";
    private static UsbDeviceConnection connection;
    private static UsbEndpoint inPoint;
    private static UsbEndpoint outPoint;
    private static UsbInterface usbInterface;

    private MicDongleHid() {
    }

    private final byte[] bulkTransfer(byte[] buffer, int timeOut) {
        byte[] bArrReceiveData;
        int i2 = 5;
        while (true) {
            int i3 = i2 - 1;
            if (i2 < 0) {
                return null;
            }
            HidUtils hidUtils = HidUtils.INSTANCE;
            if (hidUtils.sendData(connection, outPoint, buffer, buffer.length) > 0 && (bArrReceiveData = hidUtils.receiveData(connection, inPoint, buffer.length, timeOut)) != null) {
                return bArrReceiveData;
            }
            i2 = i3;
        }
    }

    private final String bytesToAscii(byte[] bytes, int offset, int dataLen) {
        if (bytes == null) {
            return null;
        }
        if ((bytes.length == 0) || offset < 0 || dataLen <= 0 || offset >= bytes.length || bytes.length - offset < dataLen) {
            return null;
        }
        while (bytes[(offset + dataLen) - 1] == ((byte) 0) && dataLen != 0) {
            dataLen--;
        }
        byte[] bArr = new byte[dataLen];
        System.arraycopy(bytes, offset, bArr, 0, dataLen);
        try {
            Charset charsetForName = Charset.forName(CHARSET);
            o.b(charsetForName, "Charset.forName(CHARSET)");
            return new String(bArr, charsetForName);
        } catch (UnsupportedEncodingException e2) {
            Log.e(TAG, e2.getMessage(), e2);
            return null;
        }
    }

    private final boolean connect(UsbManager manager, UsbDevice device) {
        HidUtils hidUtils = HidUtils.INSTANCE;
        UsbInterface usbInterface2 = hidUtils.getInterface(device);
        usbInterface = usbInterface2;
        outPoint = hidUtils.getOutEndPoint(usbInterface2);
        inPoint = hidUtils.getInEndPoint(usbInterface);
        UsbDeviceConnection usbDeviceConnectionOpenDevice = hidUtils.openDevice(manager, device);
        connection = usbDeviceConnectionOpenDevice;
        return hidUtils.lockInterface(usbDeviceConnectionOpenDevice, usbInterface);
    }

    private final void disconnect() {
        HidUtils hidUtils = HidUtils.INSTANCE;
        hidUtils.unLockInterface(connection, usbInterface);
        hidUtils.closeDevice(connection);
        connection = null;
        usbInterface = null;
        inPoint = null;
        outPoint = null;
    }

    private final boolean sendFirmwarePackage(String filePath, IUpsPlugin.IUpgradeStatusListener listener) throws IOException {
        byte[] bArr = new byte[64];
        byte b2 = 0;
        bArr[0] = 78;
        byte b3 = (byte) 138;
        boolean z2 = true;
        bArr[1] = b3;
        File file = new File(filePath);
        byte[] bArrZ2 = i.Z2(new FileInputStream(file));
        long length = file.length();
        long j2 = 0;
        float f2 = 0.0f;
        while (j2 < length) {
            long j3 = length - j2;
            int i2 = j3 <= ((long) 56) ? (int) j3 : 56;
            bArr[2] = (byte) (i2 & DnsRecord.CLASS_ANY);
            bArr[3] = b2;
            bArr[4] = (byte) (255 & j2);
            bArr[5] = (byte) (((int) (65280 & j2)) >>> 8);
            bArr[6] = (byte) (((int) (16711680 & j2)) >>> 16);
            bArr[7] = (byte) (((int) (4278190080L & j2)) >>> 24);
            System.arraycopy(bArrZ2, (int) j2, bArr, 8, i2);
            float f3 = (j2 * 0.99f) / length;
            if (f3 - f2 > 0.01d) {
                listener.onProcess(f3);
                StringBuilder sb = new StringBuilder();
                sb.append("fileSize:");
                sb.append(length);
                sb.append(",sendSize:");
                sb.append(j2);
                sb.append(",sendLength:");
                a.p0(sb, i2, TAG);
                f2 = f3;
            }
            byte[] bArrBulkTransfer = bulkTransfer(bArr, 1000);
            if (bArrBulkTransfer == null) {
                return false;
            }
            if (bArrBulkTransfer[0] != ((byte) 36) || bArrBulkTransfer[1] != b3) {
                Log.d(TAG, "sendFirmwarePackage recevie package head is wrong");
                return false;
            }
            if (!o.a(String.valueOf(bytesToAscii(bArrBulkTransfer, 8, bArrBulkTransfer.length - 8) != null ? j.y(r3, "data=") : null), "OK")) {
                Log.d(TAG, "sendFirmwarePackage receive data is not OK");
                return false;
            }
            b2 = 0;
            j2 += (long) i2;
            z2 = true;
        }
        return z2;
    }

    private final byte[] sendHidCommand(byte[] data, int timeOut) {
        byte[] bArr = new byte[64];
        int i2 = 0;
        bArr[0] = 78;
        bArr[1] = (byte) InvokeCmd.CMD_SYSTEMCTRL_GET_HDMI_CLOCK_WAKEUP_STATE;
        int length = data.length;
        int i3 = 0;
        while (i2 < length) {
            bArr[i3 + 2] = data[i2];
            i2++;
            i3++;
        }
        a.s0(a.F("sendHidCommand is "), bytesToAscii(bArr, 2, 62), TAG);
        return bulkTransfer(bArr, timeOut);
    }

    public final FileInfo CalculateFileSizeAndSum(String filePath) throws IOException {
        o.g(filePath, "filePath");
        File file = new File(filePath);
        if (!file.exists()) {
            Log.d(TAG, "file is not exit");
            return null;
        }
        long length = file.length();
        long j2 = 0;
        for (byte b2 : f.b(file)) {
            j2 = (j2 + ((long) (b2 & 255))) & 4294967295L;
        }
        Log.d(TAG, "size:" + length);
        Log.d(TAG, "total:" + j2);
        return new FileInfo(String.valueOf(length), String.valueOf(j2));
    }

    public final UsbDevice findDevice(Context context) {
        o.g(context, "context");
        Object systemService = context.getSystemService("usb");
        if (systemService == null) {
            throw new TypeCastException("null cannot be cast to non-null type android.hardware.usb.UsbManager");
        }
        for (UsbDevice usbDevice : ((UsbManager) systemService).getDeviceList().values()) {
            if (isMicDongleUsb(usbDevice)) {
                return usbDevice;
            }
        }
        Log.d(TAG, "can not find dongle");
        return null;
    }

    public final String getOtaKey(Context context) {
        String strBytesToAscii;
        o.g(context, "context");
        UsbDevice usbDeviceFindDevice = findDevice(context);
        if (usbDeviceFindDevice == null) {
            return null;
        }
        Object systemService = context.getSystemService("usb");
        if (systemService == null) {
            throw new TypeCastException("null cannot be cast to non-null type android.hardware.usb.UsbManager");
        }
        if (!connect((UsbManager) systemService, usbDeviceFindDevice)) {
            Log.d(TAG, "connect failed");
            return null;
        }
        byte[] bytes = "OTA_KEY=?".getBytes(Charsets.f9181b);
        o.b(bytes, "(this as java.lang.String).getBytes(charset)");
        byte[] bArrSendHidCommand = sendHidCommand(bytes, 1000);
        disconnect();
        if (bArrSendHidCommand == null || (strBytesToAscii = bytesToAscii(bArrSendHidCommand, 2, bArrSendHidCommand.length - 2)) == null) {
            return null;
        }
        return j.y(strBytesToAscii, "OTA_KEY=");
    }

    public final int getUpgradeProcess(Context context) {
        String str;
        o.g(context, "context");
        UsbDevice usbDeviceFindDevice = findDevice(context);
        if (usbDeviceFindDevice == null) {
            return -1;
        }
        Object systemService = context.getSystemService("usb");
        if (systemService == null) {
            throw new TypeCastException("null cannot be cast to non-null type android.hardware.usb.UsbManager");
        }
        if (connect((UsbManager) systemService, usbDeviceFindDevice)) {
            byte[] bytes = "FW_PER=?".getBytes(Charsets.f9181b);
            o.b(bytes, "(this as java.lang.String).getBytes(charset)");
            byte[] bArrSendHidCommand = sendHidCommand(bytes, 1000);
            disconnect();
            if (bArrSendHidCommand != null) {
                String strBytesToAscii = bytesToAscii(bArrSendHidCommand, 2, bArrSendHidCommand.length - 2);
                String strY = strBytesToAscii != null ? j.y(strBytesToAscii, "FW_PER=") : null;
                if (strY != null) {
                    return Integer.parseInt(strY);
                }
                return -1;
            }
            str = "getUpgradeProcess result is null";
        } else {
            str = "connect failed";
        }
        Log.d(TAG, str);
        return -1;
    }

    public final String getVersion(Context context) {
        String strBytesToAscii;
        o.g(context, "context");
        UsbDevice usbDeviceFindDevice = findDevice(context);
        if (usbDeviceFindDevice == null) {
            return null;
        }
        Object systemService = context.getSystemService("usb");
        if (systemService == null) {
            throw new TypeCastException("null cannot be cast to non-null type android.hardware.usb.UsbManager");
        }
        if (!connect((UsbManager) systemService, usbDeviceFindDevice)) {
            Log.d(TAG, "connect failed");
            return null;
        }
        byte[] bytes = "VER=?".getBytes(Charsets.f9181b);
        o.b(bytes, "(this as java.lang.String).getBytes(charset)");
        byte[] bArrSendHidCommand = sendHidCommand(bytes, 1000);
        disconnect();
        if (bArrSendHidCommand == null || (strBytesToAscii = bytesToAscii(bArrSendHidCommand, 2, bArrSendHidCommand.length - 2)) == null) {
            return null;
        }
        return j.y(strBytesToAscii, "VER=");
    }

    public final boolean isMicDongleUsb(UsbDevice device) {
        o.g(device, "device");
        return device.getVendorId() == DONGLE_VID && device.getProductId() == DONGLE_PID;
    }

    public final boolean upgradeFirmwareEnd(Context context, boolean success) {
        String strL;
        o.g(context, "context");
        UsbDevice usbDeviceFindDevice = findDevice(context);
        if (usbDeviceFindDevice == null) {
            return false;
        }
        Object systemService = context.getSystemService("usb");
        if (systemService == null) {
            throw new TypeCastException("null cannot be cast to non-null type android.hardware.usb.UsbManager");
        }
        if (connect((UsbManager) systemService, usbDeviceFindDevice)) {
            int i2 = success ? 0 : -1;
            String strL2 = a.l("FW_END=a.bin,", i2);
            Charset charset = Charsets.f9181b;
            if (strL2 == null) {
                throw new TypeCastException("null cannot be cast to non-null type java.lang.String");
            }
            byte[] bytes = strL2.getBytes(charset);
            o.b(bytes, "(this as java.lang.String).getBytes(charset)");
            byte[] bArrSendHidCommand = sendHidCommand(bytes, 1000);
            disconnect();
            if (bArrSendHidCommand == null) {
                strL = "upgradeFirmwareEnd result is null";
            } else {
                String strBytesToAscii = bytesToAscii(bArrSendHidCommand, 2, bArrSendHidCommand.length - 2);
                String strY = strBytesToAscii != null ? j.y(strBytesToAscii, "FW_END=a.bin,") : null;
                StringBuilder sbF = a.F("upgradeFirmwareEnd result:");
                sbF.append(String.valueOf(strY));
                Log.d(TAG, sbF.toString());
                if (strY != null && !(!o.a(strY, "0"))) {
                    return true;
                }
                strL = a.l("upload file fail:", i2);
            }
        } else {
            strL = "connect failed";
        }
        Log.d(TAG, strL);
        return false;
    }

    public final boolean upgradeFirmwareStart(Context context, String path) throws IOException {
        String str;
        o.g(context, "context");
        o.g(path, "path");
        UsbDevice usbDeviceFindDevice = findDevice(context);
        if (usbDeviceFindDevice == null) {
            return false;
        }
        Object systemService = context.getSystemService("usb");
        if (systemService == null) {
            throw new TypeCastException("null cannot be cast to non-null type android.hardware.usb.UsbManager");
        }
        if (connect((UsbManager) systemService, usbDeviceFindDevice)) {
            FileInfo fileInfoCalculateFileSizeAndSum = CalculateFileSizeAndSum(path);
            if (fileInfoCalculateFileSizeAndSum == null) {
                return false;
            }
            StringBuilder sbF = a.F("FW_START=a.bin,");
            sbF.append(fileInfoCalculateFileSizeAndSum.getFileSize());
            sbF.append(',');
            sbF.append(fileInfoCalculateFileSizeAndSum.getSum32());
            String string = sbF.toString();
            Charset charset = Charsets.f9181b;
            if (string == null) {
                throw new TypeCastException("null cannot be cast to non-null type java.lang.String");
            }
            byte[] bytes = string.getBytes(charset);
            o.b(bytes, "(this as java.lang.String).getBytes(charset)");
            byte[] bArrSendHidCommand = sendHidCommand(bytes, InvokeCmd.CMD_PMSCTRL_SET_PMS_WAKEUP_SOURCES);
            disconnect();
            if (bArrSendHidCommand != null) {
                a.s0(a.F("upgradeFirmwareStart result is "), bytesToAscii(bArrSendHidCommand, 2, bArrSendHidCommand.length - 2), TAG);
                String strBytesToAscii = bytesToAscii(bArrSendHidCommand, 2, bArrSendHidCommand.length - 2);
                return String.valueOf(strBytesToAscii != null ? j.y(strBytesToAscii, "FW_START=a.bin,") : null).equals("OK");
            }
            str = "upgradeFirmwareStart result is null";
        } else {
            str = "connect failed";
        }
        Log.d(TAG, str);
        return false;
    }

    public final boolean uploadFile(Context context, String filePath, IUpsPlugin.IUpgradeStatusListener listener) throws IOException {
        o.g(context, "context");
        o.g(filePath, "filePath");
        o.g(listener, "listener");
        UsbDevice usbDeviceFindDevice = findDevice(context);
        if (usbDeviceFindDevice == null) {
            return false;
        }
        Object systemService = context.getSystemService("usb");
        if (systemService == null) {
            throw new TypeCastException("null cannot be cast to non-null type android.hardware.usb.UsbManager");
        }
        if (!connect((UsbManager) systemService, usbDeviceFindDevice)) {
            Log.d(TAG, "connect failed");
            return false;
        }
        boolean zSendFirmwarePackage = sendFirmwarePackage(filePath, listener);
        disconnect();
        return zSendFirmwarePackage;
    }
}
