package com.dss.ups.sp51a.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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
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 = {"\u0000d\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0010\u0012\n\u0000\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0018\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\bÆ\u0002\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b@\u0010\u0015J\u0019\u0010\u0005\u001a\u0004\u0018\u00010\u00042\u0006\u0010\u0003\u001a\u00020\u0002H\u0002¢\u0006\u0004\b\u0005\u0010\u0006J\u0017\u0010\t\u001a\u00020\b2\u0006\u0010\u0007\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\t\u0010\nJ\u0019\u0010\r\u001a\u0004\u0018\u00010\u000b2\u0006\u0010\f\u001a\u00020\u000bH\u0002¢\u0006\u0004\b\r\u0010\u000eJ\u001f\u0010\u0011\u001a\u00020\b2\u0006\u0010\u0010\u001a\u00020\u000f2\u0006\u0010\u0007\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0011\u0010\u0012J\u000f\u0010\u0014\u001a\u00020\u0013H\u0002¢\u0006\u0004\b\u0014\u0010\u0015J!\u0010\u001a\u001a\u0004\u0018\u00010\u00162\u0006\u0010\u0017\u001a\u00020\u00162\u0006\u0010\u0019\u001a\u00020\u0018H\u0002¢\u0006\u0004\b\u001a\u0010\u001bJ\u001f\u0010\u001e\u001a\u00020\b2\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u001d\u001a\u00020\u001cH\u0002¢\u0006\u0004\b\u001e\u0010\u001fJ)\u0010\"\u001a\u0004\u0018\u00010\u00162\u0006\u0010 \u001a\u00020\u00162\u0006\u0010!\u001a\u00020\u00182\u0006\u0010\u0019\u001a\u00020\u0018H\u0002¢\u0006\u0004\b\"\u0010#J'\u0010$\u001a\u00020\b2\u0006\u0010 \u001a\u00020\u00162\u0006\u0010!\u001a\u00020\u00182\u0006\u0010\u0019\u001a\u00020\u0018H\u0002¢\u0006\u0004\b$\u0010%J+\u0010)\u001a\u0004\u0018\u00010\u000b2\b\u0010&\u001a\u0004\u0018\u00010\u00162\u0006\u0010'\u001a\u00020\u00182\u0006\u0010(\u001a\u00020\u0018H\u0002¢\u0006\u0004\b)\u0010*J\u0017\u0010+\u001a\u0004\u0018\u00010\u000b2\u0006\u0010\u0003\u001a\u00020\u0002¢\u0006\u0004\b+\u0010,J%\u0010.\u001a\u00020\b2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010-\u001a\u00020\u000b2\u0006\u0010\u001d\u001a\u00020\u001c¢\u0006\u0004\b.\u0010/J%\u00100\u001a\u00020\b2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u001d\u001a\u00020\u001c¢\u0006\u0004\b0\u0010/J\u0015\u00101\u001a\u00020\b2\u0006\u0010\u0003\u001a\u00020\u0002¢\u0006\u0004\b1\u00102R\u0016\u00103\u001a\u00020\u000b8\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b3\u00104R\u0018\u00106\u001a\u0004\u0018\u0001058\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b6\u00107R\u0016\u00108\u001a\u00020\u000b8\u0002@\u0002X\u0082T¢\u0006\u0006\n\u0004\b8\u00104R\u0018\u0010:\u001a\u0004\u0018\u0001098\u0002@\u0002X\u0082\u000e¢\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\u0018\u0010?\u001a\u0004\u0018\u0001058\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b?\u00107¨\u0006A"}, d2 = {"Lcom/dss/ups/sp51a/dongle/DongleHid;", "", "Landroid/content/Context;", "context", "Landroid/hardware/usb/UsbDevice;", "findDevice", "(Landroid/content/Context;)Landroid/hardware/usb/UsbDevice;", "device", "", "isDongleUsb", "(Landroid/hardware/usb/UsbDevice;)Z", "", "filePath", "crcFirmware", "(Ljava/lang/String;)Ljava/lang/String;", "Landroid/hardware/usb/UsbManager;", "manager", "connect", "(Landroid/hardware/usb/UsbManager;Landroid/hardware/usb/UsbDevice;)Z", "", "disconnect", "()V", "", "data", "", "timeOut", "sendHidCommand", "([BI)[B", "Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;", "listener", "sendFirmwarePackage", "(Ljava/lang/String;Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "buffer", "size", "bulkTransfer", "([BII)[B", "bulkSend", "([BII)Z", "bytes", "offset", "dataLen", "bytesToAscii", "([BII)Ljava/lang/String;", "getVersion", "(Landroid/content/Context;)Ljava/lang/String;", "path", "upgradeFirmwareStart", "(Landroid/content/Context;Ljava/lang/String;Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "uploadFile", "upgradeFirmwareEnd", "(Landroid/content/Context;)Z", "CHARSET", "Ljava/lang/String;", "Landroid/hardware/usb/UsbEndpoint;", "outPoint", "Landroid/hardware/usb/UsbEndpoint;", "TAG", "Landroid/hardware/usb/UsbInterface;", "usbInterface", "Landroid/hardware/usb/UsbInterface;", "Landroid/hardware/usb/UsbDeviceConnection;", "connection", "Landroid/hardware/usb/UsbDeviceConnection;", "inPoint", "<init>", "lib_release"}, k = 1, mv = {1, 4, 0})
public final class DongleHid {
    private static final String CHARSET = "ISO8859-1";
    public static final DongleHid INSTANCE = new DongleHid();
    private static final String TAG = "DongleHid";
    private static UsbDeviceConnection connection;
    private static UsbEndpoint inPoint;
    private static UsbEndpoint outPoint;
    private static UsbInterface usbInterface;

    private DongleHid() {
    }

    private final boolean bulkSend(byte[] buffer, int size, int timeOut) {
        int i2 = 3;
        while (true) {
            int i3 = i2 - 1;
            if (i2 < 0) {
                return false;
            }
            if (HidUtils.INSTANCE.sendData(connection, outPoint, buffer, size) > 0) {
                return true;
            }
            i2 = i3;
        }
    }

    private final byte[] bulkTransfer(byte[] buffer, int size, int timeOut) {
        byte[] bArrReceiveData;
        int i2 = 3;
        while (true) {
            int i3 = i2 - 1;
            if (i2 < 0) {
                return null;
            }
            HidUtils hidUtils = HidUtils.INSTANCE;
            if (hidUtils.sendData(connection, outPoint, buffer, size) > 0 && (bArrReceiveData = hidUtils.receiveData(connection, inPoint, 64, 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, 2);
        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 String crcFirmware(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            Log.e(TAG, "file is not exit");
            return null;
        }
        long length = file.length();
        byte[] bArrB = f.b(file);
        Crc16Util crc16Util = Crc16Util.INSTANCE;
        byte[] paramCRC = crc16Util.setParamCRC(bArrB);
        String hexStr = crc16Util.toHexStr(paramCRC);
        if (hexStr == null) {
            throw new TypeCastException("null cannot be cast to non-null type java.lang.String");
        }
        String upperCase = hexStr.toUpperCase();
        o.b(upperCase, "(this as java.lang.String).toUpperCase()");
        Log.i(TAG, "crc16: " + ((int) paramCRC[0]) + ", " + ((int) paramCRC[1]) + ", " + upperCase);
        StringBuilder sb = new StringBuilder();
        sb.append("size: ");
        sb.append(length);
        Log.i(TAG, sb.toString());
        Log.i(TAG, "byteCrc: " + upperCase);
        return upperCase;
    }

    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 UsbDevice findDevice(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 (isDongleUsb(usbDevice)) {
                return usbDevice;
            }
        }
        Log.e(TAG, "can not find dongle");
        return null;
    }

    private final boolean isDongleUsb(UsbDevice device) {
        StringBuilder sbF = a.F("vid:");
        sbF.append(device.getVendorId());
        sbF.append(", pid:");
        sbF.append(device.getProductId());
        Log.i(TAG, sbF.toString());
        return device.getVendorId() == 9354 && device.getProductId() == 34845;
    }

    private final boolean sendFirmwarePackage(String filePath, IUpsPlugin.IUpgradeStatusListener listener) throws InterruptedException, IOException {
        byte[] bArr = new byte[64];
        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) 64) ? 64 : (int) j3;
            System.arraycopy(bArrZ2, (int) j2, bArr, 0, i2);
            float f3 = (j2 * 0.99f) / length;
            if (f3 - f2 > 0.05d) {
                listener.onProcess(f3);
                StringBuilder sb = new StringBuilder();
                sb.append("progress:");
                sb.append(f3);
                sb.append(", fileSize:");
                sb.append(length);
                sb.append(", sendSize:");
                sb.append(j2);
                sb.append(", sendLength:");
                a.q0(sb, i2, TAG);
                f2 = f3;
            }
            if (!bulkSend(bArr, i2, 1000)) {
                return false;
            }
            j2 += (long) i2;
            Thread.sleep(15L);
        }
        return true;
    }

    private final byte[] sendHidCommand(byte[] data, int timeOut) {
        byte[] bArr = new byte[64];
        int length = data.length;
        int i2 = 0;
        int i3 = 0;
        while (i2 < length) {
            bArr[i3] = data[i2];
            i2++;
            i3++;
        }
        StringBuilder sbF = a.F("sendHidCommand is: ");
        sbF.append(bytesToAscii(bArr, 0, 64));
        Log.i(TAG, sbF.toString());
        return bulkTransfer(bArr, data.length, timeOut);
    }

    public final String getVersion(Context context) {
        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.e(TAG, "connect failed");
            return null;
        }
        byte[] bytes = "VERcmd".getBytes(Charsets.f9181b);
        o.b(bytes, "(this as java.lang.String).getBytes(charset)");
        byte[] bArrSendHidCommand = sendHidCommand(bytes, 1000);
        disconnect();
        if (bArrSendHidCommand == null || bArrSendHidCommand[0] != 90) {
            return null;
        }
        String strValueOf = String.valueOf((int) bArrSendHidCommand[2]);
        Log.i(TAG, "get:" + strValueOf);
        return strValueOf;
    }

    public final boolean upgradeFirmwareEnd(Context context) {
        String str;
        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)) {
            byte[] bytes = "OTAend".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, 0, bArrSendHidCommand.length);
                StringBuilder sbK = a.K("upgradeFirmwareEnd result: ", strBytesToAscii, ", ");
                String string = Arrays.toString(bArrSendHidCommand);
                o.b(string, "java.util.Arrays.toString(this)");
                sbK.append(string);
                Log.i(TAG, sbK.toString());
                if (strBytesToAscii != null && j.d(strBytesToAscii, "OT", false, 2) && bArrSendHidCommand[3] == 17) {
                    Log.e(TAG, "upload file success");
                    return true;
                }
                Log.e(TAG, "upload file fail");
                return false;
            }
            str = "upgradeFirmwareEnd result is null";
        } else {
            str = "connect failed";
        }
        Log.e(TAG, str);
        return false;
    }

    public final boolean upgradeFirmwareStart(Context context, String path, IUpsPlugin.IUpgradeStatusListener listener) throws IOException {
        String str;
        o.g(context, "context");
        o.g(path, "path");
        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)) {
            String strCrcFirmware = crcFirmware(path);
            if (strCrcFirmware == null) {
                return false;
            }
            Charset charset = Charsets.f9181b;
            byte[] bytes = strCrcFirmware.getBytes(charset);
            o.b(bytes, "(this as java.lang.String).getBytes(charset)");
            StringBuilder sb = new StringBuilder();
            sb.append("crc: ");
            sb.append(strCrcFirmware);
            sb.append(", byte[");
            sb.append(bytes.length);
            sb.append("]:");
            String string = Arrays.toString(bytes);
            o.b(string, "java.util.Arrays.toString(this)");
            sb.append(string);
            Log.i(TAG, sb.toString());
            byte[] bytes2 = "OTAMode0X".getBytes(charset);
            o.b(bytes2, "(this as java.lang.String).getBytes(charset)");
            byte[] bArrU = kotlin.collections.j.U(bytes2, bytes);
            boolean zBulkSend = bulkSend(bArrU, bArrU.length, InvokeCmd.CMD_PMSCTRL_SET_PMS_WAKEUP_SOURCES);
            disconnect();
            if (zBulkSend) {
                listener.onProcess(0.01f);
                return true;
            }
            str = "upgradeFirmwareStart result is false";
        } else {
            str = "connect failed";
        }
        Log.e(TAG, str);
        return false;
    }

    public final boolean uploadFile(Context context, String filePath, IUpsPlugin.IUpgradeStatusListener listener) {
        String str;
        o.g(context, "context");
        o.g(filePath, "filePath");
        o.g(listener, "listener");
        Log.i(TAG, "uploadFile: " + filePath);
        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)) {
            str = "connect failed";
        } else {
            if (sendFirmwarePackage(filePath, listener)) {
                disconnect();
                return true;
            }
            str = "sendFirmwarePackage failed";
        }
        Log.e(TAG, str);
        return false;
    }
}
