package g.i.a.c.b0;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.text.TextUtils;
import com.cvte.vman.instance.InvokeCmd;
import com.realsil.sdk.core.usb.GlobalUsbGatt;
import com.realsil.sdk.core.usb.UsbGatt;
import com.realsil.sdk.core.usb.UsbGattCallback;
import com.realsil.sdk.core.usb.UsbGattCharacteristic;
import com.realsil.sdk.core.usb.UsbGattImpl;
import g.h.a.i;
import g.i.a.c.q.f;
import g.i.a.c.v.g;
import io.netty.handler.codec.compression.Bzip2Constants;
import io.netty.handler.codec.dns.DnsRecord;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import no.nordicsemi.android.dfu.DfuServiceInitiator;

/* JADX INFO: loaded from: classes.dex */
public class b extends c {
    public UUID n0;
    public UUID o0;
    public final UsbGattCallback p0;

    public class a extends UsbGattCallback {
        public a(b bVar) {
        }
    }

    public b(Context context, f fVar, g.i.a.c.m.a.b bVar) {
        super(context, fVar, bVar);
        this.n0 = g.f5752b;
        this.o0 = g.f5753c;
        this.p0 = new a(this);
    }

    public final boolean P() {
        try {
            i.x0(this.f5594f, "<< OPCODE_DFU_RESET_SYSTEM (0x05)");
            return L(null, new byte[]{5}, true);
        } catch (g.i.a.c.a e2) {
            i.w0(String.format("Send OPCODE_DFU_RESET_SYSTEM failed, ignore it, errorcode= 0x%04X", Integer.valueOf(e2.f5446m)));
            this.M = 0;
            return false;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:115:0x042f, code lost:
    
        r9 = 17;
        r5 = 16;
        r0 = r8;
        r1 = r10;
        r2 = r11;
        r3 = r13;
        r4 = r14;
        r15 = r20;
        r13 = r21;
        r14 = 0;
     */
    /* JADX WARN: Removed duplicated region for block: B:110:0x03ff  */
    /* JADX WARN: Removed duplicated region for block: B:111:0x0411  */
    /* JADX WARN: Removed duplicated region for block: B:114:0x042d A[EDGE_INSN: B:114:0x042d->B:202:0x042f BREAK  A[LOOP:1: B:91:0x0314->B:116:0x043f]] */
    /* JADX WARN: Removed duplicated region for block: B:179:0x05c7  */
    /* JADX WARN: Removed duplicated region for block: B:180:0x05d0  */
    /* JADX WARN: Removed duplicated region for block: B:183:0x0613 A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:184:0x0614  */
    /* JADX WARN: Removed duplicated region for block: B:201:0x0453 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:52:0x01d4  */
    /* JADX WARN: Removed duplicated region for block: B:55:0x01f7  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x024d  */
    /* JADX WARN: Removed duplicated region for block: B:67:0x0254  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean Q() throws g.i.a.c.g.c {
        /*
            Method dump skipped, instruction units count: 1613
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.b0.b.Q():boolean");
    }

    public final int R(String str, int i2) {
        int i3 = 0;
        while (!this.f5600r) {
            int iV = V(str);
            if (iV == 0) {
                return 0;
            }
            if ((iV & (-2049)) != 133) {
                M(this.j0);
            } else {
                i.W3("connect fail with GATT_ERROR, do not need disconnect");
            }
            H(this.j0);
            try {
                Thread.sleep(1600L);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
            i3++;
            g.a.a.a.a.a0("tryConnectTime=", i3);
            if (i3 > i2) {
                return iV;
            }
        }
        return InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO;
    }

    public final void S(int i2, int i3) {
        byte[] bArr = {2, (byte) (i2 & DnsRecord.CLASS_ANY), (byte) ((i2 >> 8) & DnsRecord.CLASS_ANY), (byte) (i3 & DnsRecord.CLASS_ANY), (byte) ((i3 >> 8) & DnsRecord.CLASS_ANY), (byte) ((i3 >> 16) & DnsRecord.CLASS_ANY), (byte) ((i3 >> 24) & DnsRecord.CLASS_ANY)};
        i.w0("<< OPCODE_DFU_RECEIVE_FW_IMAGE (0x02)");
        L(null, bArr, false);
    }

    public final void T(int i2, boolean z2) {
        if (this.f5600r) {
            i2 = 4128;
        }
        if (i2 != 4128) {
            z(260, true);
        }
        i.U3(String.format("error = 0x%04X, needReset=%b", Integer.valueOf(i2), Boolean.valueOf(z2)));
        if (z2) {
            P();
        }
        k(this.G);
        if (r().f(1)) {
            N(i2);
        }
        g.i.a.c.m.a.b bVar = this.f5598p;
        if (bVar != null) {
            bVar.a(i2);
        }
        this.f5600r = true;
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0037  */
    /* JADX WARN: Removed duplicated region for block: B:19:0x0047  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void U(byte[] r5) throws g.i.a.c.g.c {
        /*
            r4 = this;
            r0 = 524(0x20c, float:7.34E-43)
            r1 = 1
            r4.z(r0, r1)
            boolean r0 = r4.f5600r
            r2 = 4128(0x1020, float:5.785E-42)
            if (r0 != 0) goto L4d
            r0 = 0
            java.lang.String r3 = "<< OPCODE_DFU_ACTIVE_IMAGE_RESET(0x04)"
            g.h.a.i.w0(r3)     // Catch: g.i.a.c.a -> L18
            r3 = 0
            boolean r1 = r4.L(r3, r5, r0)     // Catch: g.i.a.c.a -> L18
            goto L2b
        L18:
            r5 = move-exception
            int r3 = r5.f5446m
            if (r3 != r2) goto L1e
            goto L35
        L1e:
            g.i.a.c.q.f r2 = r4.r()
            boolean r2 = r2.f5709z
            if (r2 != 0) goto L2e
            java.lang.String r5 = "active cmd has no response, ignore"
            g.h.a.i.w0(r5)
        L2b:
            r2 = r0
            r0 = r1
            goto L35
        L2e:
            java.lang.String r1 = "active cmd has no response, notify error"
            g.h.a.i.W3(r1)
            int r2 = r5.f5446m
        L35:
            if (r0 == 0) goto L47
            java.lang.String r5 = "image active success"
            g.h.a.i.w0(r5)
            int r5 = r4.M
            r4.N(r5)
            g.i.a.c.l.e.a r5 = r4.G
            r4.k(r5)
            return
        L47:
            g.i.a.c.g.c r5 = new g.i.a.c.g.c
            r5.<init>(r2)
            throw r5
        L4d:
            g.i.a.c.g.c r5 = new g.i.a.c.g.c
            java.lang.String r0 = "user aborted"
            r5.<init>(r0, r2)
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.b0.b.U(byte[]):void");
    }

    public final int V(String str) {
        UsbDevice usbDevice;
        StringBuilder sb;
        int i2;
        B(256);
        this.M = 0;
        this.f5603u = false;
        i.x0(this.f5594f, "Connecting to device..." + str);
        UsbGatt usbGattConnectGatt = null;
        if (TextUtils.isEmpty(str)) {
            usbDevice = null;
        } else {
            UsbManager usbManager = this.h0;
            if (usbManager == null) {
                i.W3("mUsbManager == null");
            } else {
                HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
                if (deviceList == null || deviceList.size() <= 0) {
                    i.w0("no usb device exist");
                } else {
                    usbDevice = deviceList.get(str);
                }
            }
            usbDevice = null;
        }
        if (usbDevice == null) {
            i.W3("device is null");
            this.j0 = null;
            return 256;
        }
        GlobalUsbGatt globalUsbGatt = this.i0;
        if (globalUsbGatt != null) {
            globalUsbGatt.unRegisterCallback(str, this.p0);
            if (this.i0.connect(usbDevice, this.f5595m, this.p0)) {
                usbGattConnectGatt = this.i0.getBluetoothGatt(str);
                this.j0 = usbGattConnectGatt;
                try {
                    synchronized (this.f5604v) {
                        if (!this.f5603u && this.M == 0) {
                            i.x0(this.f5594f, "wait for connect gatt for 32000 ms");
                            this.f5604v.wait(32000L);
                        }
                    }
                } catch (InterruptedException e2) {
                    e = e2;
                    sb = new StringBuilder();
                    sb.append("Sleeping interrupted : ");
                    sb.append(e.toString());
                    i.D0(sb.toString());
                    this.M = 259;
                }
            } else {
                this.M = 256;
            }
        } else {
            usbGattConnectGatt = UsbGattImpl.connectGatt(usbDevice, this.f5595m, this.p0);
            this.j0 = usbGattConnectGatt;
            try {
                synchronized (this.f5604v) {
                    if (!this.f5603u && this.M == 0) {
                        i.x0(this.f5594f, "wait for connect gatt for 32000 ms");
                        this.f5604v.wait(32000L);
                    }
                }
            } catch (InterruptedException e3) {
                e = e3;
                sb = new StringBuilder();
                sb.append("Sleeping interrupted : ");
                sb.append(e.toString());
                i.D0(sb.toString());
                this.M = 259;
            }
        }
        if (this.M == 0) {
            if (!this.f5603u) {
                i.W3("wait for connect, but can not connect with no callback");
                i2 = 260;
            } else if (usbGattConnectGatt == null || this.f5605w != 515) {
                StringBuilder sbF = g.a.a.a.a.F("connect with some error, please check. mConnectionState=");
                sbF.append(this.f5605w);
                i.W3(sbF.toString());
                i2 = 264;
            }
            this.M = i2;
        }
        if (this.M == 0) {
            i.V3(this.f5594f, "connected the device which going to upgrade");
        }
        return this.M;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:14:0x005a A[Catch: IOException -> 0x029f, TryCatch #0 {IOException -> 0x029f, blocks: (B:10:0x004c, B:12:0x0051, B:15:0x0067, B:17:0x006b, B:19:0x007e, B:21:0x0085, B:24:0x0097, B:26:0x00b1, B:28:0x00b5, B:30:0x00ce, B:23:0x008f, B:25:0x009f, B:14:0x005a), top: B:84:0x004c }] */
    /* JADX WARN: Removed duplicated region for block: B:30:0x00ce A[Catch: IOException -> 0x029f, TRY_LEAVE, TryCatch #0 {IOException -> 0x029f, blocks: (B:10:0x004c, B:12:0x0051, B:15:0x0067, B:17:0x006b, B:19:0x007e, B:21:0x0085, B:24:0x0097, B:26:0x00b1, B:28:0x00b5, B:30:0x00ce, B:23:0x008f, B:25:0x009f, B:14:0x005a), top: B:84:0x004c }] */
    /* JADX WARN: Removed duplicated region for block: B:38:0x0113  */
    /* JADX WARN: Removed duplicated region for block: B:92:0x00fa A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void W(com.realsil.sdk.core.usb.UsbGatt r17, com.realsil.sdk.core.usb.UsbGattCharacteristic r18, g.i.a.c.l.e.a r19) throws g.i.a.c.g.c {
        /*
            Method dump skipped, instruction units count: 688
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.b0.b.W(com.realsil.sdk.core.usb.UsbGatt, com.realsil.sdk.core.usb.UsbGattCharacteristic, g.i.a.c.l.e.a):void");
    }

    public final byte[] X(long j2) throws g.i.a.c.g.c {
        this.M = 0;
        this.k0 = true;
        try {
            synchronized (this.V) {
                if (this.M == 0 && this.f5605w == 515) {
                    this.k0 = false;
                    i.V3(this.f5594f, "wait for notification, wait for " + j2 + "ms");
                    this.V.wait(j2);
                }
                if (this.M == 0 && !this.k0) {
                    i.W3("wait for notification, but not come");
                    this.M = 767;
                }
            }
        } catch (InterruptedException e2) {
            StringBuilder sbF = g.a.a.a.a.F("readNotificationResponse interrupted, ");
            sbF.append(e2.toString());
            i.D0(sbF.toString());
            this.M = 259;
        }
        if (this.M == 0) {
            return null;
        }
        throw new g.i.a.c.g.c("Unable to receive notification", this.M);
    }

    public final void Y(UsbGatt usbGatt, UsbGattCharacteristic usbGattCharacteristic, g.i.a.c.l.e.a aVar) throws g.i.a.c.g.c {
        int iG;
        i.x0(this.f5594f, "uploadFirmwareImageForBeeUpdate");
        j();
        this.M = 0;
        this.D = false;
        int i2 = this.U;
        byte[] bArr = new byte[i2];
        while (!this.D) {
            if (this.f5600r) {
                throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
            }
            C();
            if (this.f5594f) {
                i.U3(s().toString());
            }
            try {
                if (this.Z == 0) {
                    int i3 = this.U;
                    byte[] bArr2 = new byte[i3];
                    aVar.g(bArr2, i3 - 12);
                    System.arraycopy(aVar.f5567r, 0, bArr, 0, 12);
                    System.arraycopy(bArr2, 0, bArr, 12, this.U - 12);
                    iG = this.U;
                } else {
                    iG = aVar.g(bArr, i2);
                }
                if (s().d() < this.U) {
                    i.U3("reach the end of the file, only read some");
                    iG = s().d();
                }
                int i4 = iG;
                if (i4 <= 0) {
                    if (s().o()) {
                        i.V1("image file has already been send over");
                        return;
                    }
                    i.D0("Error while reading file with size: " + i4);
                    throw new g.i.a.c.g.c("Error while reading file", 257);
                }
                if (t().f5681q) {
                    for (int i5 = i4; i5 > 0; i5 -= 16) {
                        if (i5 >= 16) {
                            int i6 = i4 - i5;
                            System.arraycopy(this.L.a(bArr, i6, 16), 0, bArr, i6, 16);
                            if (t().f5682r == 0) {
                                break;
                            }
                        }
                    }
                }
                K(usbGatt, null, bArr, i4, false);
                E();
                i();
            } catch (IOException unused) {
                throw new g.i.a.c.g.c("Error while reading file", 257);
            }
        }
    }

    public final void Z(int i2) {
        boolean z2;
        String str;
        int i3 = this.Z;
        if (i3 == 0) {
            this.Z = 12;
            z2 = this.f5594f;
            str = String.format(Locale.US, "First Packet, mImageUpdateOffset=0x%08X(%d)", 12, Integer.valueOf(this.Z));
        } else {
            z2 = this.f5594f;
            str = String.format(Locale.US, "mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(i3), Integer.valueOf(this.Z));
        }
        i.x0(z2, str);
        S(i2, this.Z);
        int i4 = s().f5711m;
        int i5 = this.Z;
        if (i4 == i5 || i5 == -1) {
            return;
        }
        i.w0("mBytesSent != mImageUpdateOffset, reload image bin file");
        G();
        g(this.Z, false);
    }

    public final void a0() throws g.i.a.c.g.c {
        U(new byte[]{4});
    }

    public final boolean b0() throws g.i.a.c.g.c {
        String str = this.O;
        if (this.f5600r) {
            throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
        }
        z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_SET, true);
        int iR = R(str, r().M);
        if (iR != 0) {
            if (iR == 4128) {
                throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iR);
            }
            throw new g.i.a.c.g.c("connectRemoteDevice failed", iR);
        }
        g.a.a.a.a.u0(this.o0, g.a.a.a.a.F("not found DFU_CONTROL_POINT_UUID : "));
        if (this.f5601s) {
            F();
        } else {
            this.f5601s = false;
            z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_GET, true);
            try {
                Thread.sleep(1500L);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
            this.W = new g.i.a.c.q.i(this.f5596n, 2);
            if (t().f5674j != 5 && t().f5674j != 9 && t().f5674j != 4 && t().f5674j != 6 && t().f5674j != 7 && t().f5674j != 8 && t().f5674j != 11) {
                int i2 = t().f5674j;
            }
            if (t().f5675k != 0) {
                g.i.a.c.q.i iVarT = t();
                iVarT.P = null;
                iVarT.i();
                i.W3("no ImageVersionCharacteristics to read");
            }
            if (this.f5594f) {
                i.w0(t().toString());
            }
            G();
            List<g.i.a.c.l.e.a> list = this.F;
            if (list != null && list.size() > 0) {
                Iterator<g.i.a.c.l.e.a> it = this.F.iterator();
                while (it.hasNext()) {
                    int i3 = it.next().f5573x;
                }
            }
            this.f5601s = true;
            i.w0("Ota Environment prepared.");
        }
        if (this.G != null) {
            return true;
        }
        T(4097, false);
        return false;
    }

    public final boolean c0() {
        g.i.a.c.l.e.a aVar;
        int i2;
        z(InvokeCmd.CMD_XBUG_GET_VMAN_SERVICE_LOG_LEVEL, true);
        this.O = this.P;
        this.f5602t = this.S != 0;
        boolean z2 = false;
        while (!this.f5600r) {
            try {
            } catch (g.i.a.c.a e2) {
                g.a.a.a.a.R(this.E, new StringBuilder(), ", ", e2);
                int i3 = e2.f5446m;
                if (i3 == 4128) {
                    T(i3, true);
                } else {
                    if (i3 != 4097 && i3 != 265) {
                        P();
                    }
                    T(i3, false);
                }
            }
            if (!b0() || !Q()) {
                return false;
            }
            this.J += s().f5711m;
            if (s().p()) {
                i.w0("no pendding image file to upload.");
                s().f5721w = this.J;
                if (this.T) {
                    a0();
                    i2 = Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
                } else {
                    i2 = 523;
                }
                z(i2, true);
                z2 = true;
            } else {
                i.w0("has pendding image file to upload");
                if (t().M == 1) {
                    this.O = this.P;
                    this.f5602t = this.S != 0;
                    this.J = 0;
                    a0();
                } else if (t().M == 3 && (aVar = this.H) != null) {
                    int i4 = this.J;
                    int i5 = t().C * 4096;
                    i.V3(this.f5594f, String.format(Locale.US, "nextBinSize=%d, mBytesSentBuffer=%d, bufferSize=%d", Integer.valueOf(aVar.h()), Integer.valueOf(i4), Integer.valueOf(i5)));
                    if (aVar.h() + i4 > i5) {
                        i.V1("make device to enter the ota advertiser mode, and let the app continue update image");
                        this.f5602t = true;
                        this.J = 0;
                        U(new byte[]{4, 1});
                    }
                }
                c(DfuServiceInitiator.DEFAULT_SCAN_TIMEOUT);
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e3) {
                e3.printStackTrace();
            }
            if (z2) {
                return z2;
            }
        }
        T(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, true);
        return false;
    }

    public final byte[] d0() {
        return X(r().h0);
    }

    @Override // g.i.a.c.m.a.a
    public boolean f(boolean z2) {
        if (!super.f(z2)) {
            return false;
        }
        if (this.f5605w != 515) {
            boolean z3 = this.f5594f;
            StringBuilder sbF = g.a.a.a.a.F("start to re-connect the RCU which going to active image, current state is: ");
            sbF.append(this.f5605w);
            i.x0(z3, sbF.toString());
            int iR = R(this.O, r().M);
            if (iR != 0) {
                StringBuilder sbG = g.a.a.a.a.G("Something error in OTA process, errorCode: ", iR, "mProcessState");
                sbG.append(this.E);
                i.D0(sbG.toString());
                T(iR, true);
                return false;
            }
        }
        if (z2) {
            try {
                a0();
                z(Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE, true);
            } catch (g.i.a.c.a e2) {
                e2.printStackTrace();
                T(e2.f5446m, false);
            }
        } else {
            if (P()) {
                if (!(this.S == 16)) {
                    D();
                }
            }
            T(274, false);
        }
        return true;
    }

    @Override // g.i.a.c.m.a.a
    public void o() {
        int iV;
        try {
            setName("ProcessorXU0000");
            i.V1("ProcessorXU0000 running.");
            iV = v();
        } catch (Exception e2) {
            e2.printStackTrace();
            i.D0(e2.toString());
            T(0, false);
        }
        if (iV != 0) {
            T(iV, false);
            return;
        }
        c0();
        k(this.G);
        i.x0(this.f5594f, "GattDfuTaskX0000 stopped");
        if (this.E == 525) {
            z(259, true);
        }
    }

    @Override // g.i.a.c.b0.c, g.i.a.c.b0.a, g.i.a.c.m.a.a
    public void u() {
        super.u();
        try {
            this.n0 = UUID.fromString(r().T);
            this.o0 = UUID.fromString(r().U);
        } catch (Exception e2) {
            e2.printStackTrace();
            i.D0(e2.toString());
        }
        this.f5599q = true;
    }
}
