package g.i.a.c.z;

import android.content.Context;
import com.cvte.vman.instance.InvokeCmd;
import io.netty.handler.codec.compression.Bzip2Constants;
import io.netty.handler.codec.dns.DnsRecord;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import no.nordicsemi.android.dfu.DfuBaseService;

/* JADX INFO: loaded from: classes.dex */
public class n extends k {
    public g.i.a.c.u.a v0;
    public int w0;

    public n(Context context, g.i.a.c.q.f fVar, g.i.a.c.u.a aVar, g.i.a.c.m.a.b bVar) {
        super(context, fVar, bVar);
        this.v0 = aVar;
    }

    public final boolean A0(int i2) {
        List<g.i.a.c.l.e.a> list = this.F;
        if (list == null) {
            return false;
        }
        Iterator<g.i.a.c.l.e.a> it = list.iterator();
        while (it.hasNext()) {
            if (it.next().f5570u == i2) {
                g.h.a.i.V3(this.f5594f, String.format("image 0x%04X has been packed, no need to force copy", Integer.valueOf(i2)));
                return false;
            }
        }
        g.i.a.c.l.f.a aVarA = new g.i.a.c.q.e(t()).a(i2);
        if (aVarA == null || aVarA.f5579o == -1) {
            return false;
        }
        g.h.a.i.V3(this.f5594f, String.format(Locale.US, "image 0x%04X has not been packed, and SOC image is: 0x%08X, need to forcy copy", Integer.valueOf(i2), Integer.valueOf(aVarA.f5579o)));
        return true;
    }

    public final void B0(int i2) {
        if (this.Z == 0 && t().f5675k < 3) {
            this.Z = 12;
        }
        g.h.a.i.x0(this.f5594f, String.format(Locale.US, "mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(this.Z), Integer.valueOf(this.Z)));
        int i3 = s().f5711m;
        int i4 = this.Z;
        if (i3 == i4 || i4 == -1) {
            return;
        }
        g.h.a.i.W3("mBytesSent != mImageUpdateOffset, reload image bin file");
        K();
        g(this.Z, false);
    }

    public final boolean C0() throws g.i.a.c.g.c {
        g0(this.O);
        if (this.f5601s) {
            J();
        } else {
            this.f5601s = false;
            if (!this.f5600r) {
                z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_GET, true);
                try {
                    Thread.sleep(1500L);
                } catch (InterruptedException e2) {
                    e2.printStackTrace();
                }
                g.i.a.c.u.a aVar = this.v0;
                if (aVar != null) {
                    try {
                        L((short) 1554, new byte[]{(byte) (aVar.f5751f & DnsRecord.CLASS_ANY)});
                    } catch (Exception unused) {
                        g.h.a.i.U3("CMD_OTA_TEST error, ignore");
                    }
                }
                this.W = new g.i.a.c.q.i(17, 2);
                r0();
                if (t().f5686v && t().f5675k < 6) {
                    t0();
                    if (t().f5688x != 0) {
                        throw new g.i.a.c.g.c("rws state not ready", 282);
                    }
                }
                o0();
                if (t().N && t().f5675k >= 5) {
                    q0();
                }
                if (t().M == 3) {
                    p0();
                }
                if (this.f5594f) {
                    g.h.a.i.w0(t().toString());
                }
                K();
                this.f5601s = true;
                g.h.a.i.w0("Ota Environment prepared.");
            } else {
                X(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, false);
            }
        }
        if (this.G != null) {
            return true;
        }
        X(4097, false);
        return false;
    }

    public final void D0() {
        g.h.a.i.x0(this.f5594f, "force copy data image from active bank from inactive bank...");
        if (A0(10132)) {
            z0(10132);
        }
        if (A0(10133)) {
            z0(10133);
        }
        if (A0(10134)) {
            z0(10134);
        }
        if (A0(10135)) {
            z0(10135);
        }
        if (A0(10136)) {
            z0(10136);
        }
        if (A0(10137)) {
            z0(10137);
        }
        if (A0(10138)) {
            z0(10138);
        }
    }

    @Override // g.i.a.c.x.a, g.i.a.c.o.b
    public void J() {
        int i2 = s().f5713o;
        int i3 = s().f5716r;
        if (i3 < 0 || i3 >= i2) {
            g.h.a.i.U3("invalid FileIndex: " + i3 + ", reset to 0");
            i3 = 0;
        }
        s().f5714p = i3;
        g.i.a.c.l.e.a aVar = this.F.get(i3);
        this.G = aVar;
        if (aVar != null) {
            if (this.f5594f) {
                g.h.a.i.U3(String.format("mCurBinInputStream's binId=0x%04X", Integer.valueOf(aVar.f5575z)));
            }
            g.i.a.c.q.g gVarS = s();
            g.i.a.c.l.e.a aVar2 = this.G;
            gVarS.n(aVar2.f5575z, aVar2.f5570u, aVar2.f5572w, aVar2.h() + 12, r().f5707x);
        } else {
            g.h.a.i.V3(false, "mCurBinInputStream == null");
        }
        int i4 = i3 + 1;
        if (i4 < i2) {
            this.H = this.F.get(i4);
            this.I = i4;
        } else {
            this.H = null;
            this.I = -1;
        }
    }

    @Override // g.i.a.c.m.a.a
    public void g(int i2, boolean z2) {
        if (i2 != 0) {
            try {
                int iMax = Math.max(i2 - 12, 0);
                byte[] bArr = new byte[s().f5710f];
                if (z2) {
                    this.G.g(bArr, iMax);
                } else {
                    this.G.read(bArr, 0, iMax);
                }
            } catch (IOException e2) {
                g.h.a.i.W3(e2.toString());
                return;
            }
        }
        s().r(i2);
    }

    @Override // g.i.a.c.m.a.a
    public void o() {
        int iV;
        int i2;
        try {
            setName("ProcessorXS0011");
            g.h.a.i.w0("ProcessorXS0011 running.");
            iV = v();
        } catch (g.i.a.c.a e2) {
            g.h.a.i.W3(e2.toString());
            X(e2.f5446m, false);
        } catch (Exception e3) {
            g.h.a.i.W3(e3.toString());
            X(0, false);
        }
        if (iV != 0) {
            X(iV, false);
            return;
        }
        z(InvokeCmd.CMD_XBUG_GET_VMAN_SERVICE_LOG_LEVEL, true);
        this.O = this.P;
        this.f5602t = true;
        this.J = 0;
        if (u0()) {
            g.h.a.i.U3("RWS, no need to disconnect manully");
            y0();
            g.h.a.i.w0("start to upload secondary bud ...");
            this.f5602t = true;
            this.f5601s = false;
            this.J = 0;
            if (u0()) {
                if (this.T) {
                    Y(true);
                    if (r().d(1)) {
                        g.i.a.b.e.b.a.c(this.i0.getRemoteDevice(this.O));
                        g.i.a.b.e.b.a.d(this.O);
                    }
                    i2 = Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
                } else {
                    i2 = 523;
                }
                z(i2, true);
            }
        }
        k(this.G);
        g.h.a.i.x0(this.f5594f, "DfuThread stopped");
        if (this.E == 525) {
            z(259, true);
        }
    }

    public final void t0() {
        g.h.a.i.x0(this.f5594f, String.format("<< CMD_OTA_GET_OTHER_INFO (0x%04X)", (short) 1547));
        byte[] bArrE0 = e0((short) 1547, null);
        if (bArrE0 == null || bArrE0.length <= 0) {
            t().f5688x = 0;
        } else {
            t().f5688x = bArrE0[0] & 1;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:35:0x00ae, code lost:
    
        if (r7 == 1) goto L36;
     */
    /* JADX WARN: Removed duplicated region for block: B:31:0x00a5  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x00aa A[Catch: a -> 0x00fd, TRY_ENTER, TryCatch #1 {a -> 0x00fd, blocks: (B:7:0x000e, B:10:0x0015, B:12:0x0020, B:14:0x0028, B:16:0x002c, B:18:0x005b, B:20:0x006d, B:22:0x0075, B:24:0x0079, B:26:0x0085, B:29:0x008f, B:34:0x00aa, B:36:0x00b0, B:47:0x00d3, B:51:0x00dc, B:53:0x00e4, B:55:0x00ec, B:38:0x00b6, B:42:0x00c1, B:44:0x00c7), top: B:75:0x000e }] */
    /* JADX WARN: Removed duplicated region for block: B:37:0x00b4  */
    /* JADX WARN: Removed duplicated region for block: B:50:0x00da  */
    /* JADX WARN: Removed duplicated region for block: B:79:0x00d9 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public boolean u0() {
        /*
            Method dump skipped, instruction units count: 289
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.z.n.u0():boolean");
    }

    public final int v0() {
        g.i.a.c.l.e.a aVar;
        g.i.a.c.q.g gVarS;
        z(521, true);
        s().s();
        if (this.f5594f) {
            g.h.a.i.U3("processCopyProcedure ...");
            g.h.a.i.U3(s().toString());
        }
        try {
            f0(s().f5718t);
            s().q();
            x();
            this.J += s().f5711m;
            if (s().p()) {
                g.h.a.i.V3(false, "no pendding image file to upload");
                s().f5721w = this.J;
                return 3;
            }
            g.h.a.i.w0("has pendding image file to upload");
            if (t().M == 1) {
                this.O = this.P;
                this.f5602t = true;
                this.J = 0;
                Y(true);
                gVarS = s();
            } else {
                if (t().M != 3 || (aVar = this.H) == null || !Z(aVar, this.J, t().C * 4096)) {
                    return 2;
                }
                g.h.a.i.w0("make device to enter the ota advertiser mode, and let the app continue update image");
                this.f5602t = true;
                this.J = 0;
                V((byte) 1, true);
                gVarS = s();
            }
            int i2 = gVarS.f5721w / 30;
            c(30000L);
            return 2;
        } catch (g.i.a.c.a unused) {
            return 1;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:155:0x042a A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:156:0x042b  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x016a  */
    /* JADX WARN: Type inference failed for: r2v25 */
    /* JADX WARN: Type inference failed for: r2v38 */
    /* JADX WARN: Type inference failed for: r2v8, types: [boolean] */
    /* JADX WARN: Type inference failed for: r4v22 */
    /* JADX WARN: Type inference failed for: r4v23, types: [boolean, byte] */
    /* JADX WARN: Type inference failed for: r4v24 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final int w0() throws g.i.a.c.g.c {
        /*
            Method dump skipped, instruction units count: 1303
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.z.n.w0():int");
    }

    public final int x0() {
        g.i.a.c.q.g gVarS;
        if (!(!this.f5600r)) {
            X(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, false);
            return 0;
        }
        z(521, true);
        g.h.a.i.x0(this.f5594f, String.format("mOtaWorkMode=0x%04X, ICType=%2X", Integer.valueOf(this.S), Integer.valueOf(t().f5674j)));
        g.h.a.i.V3(this.f5594f, s().toString());
        s().s();
        i0(s().f5718t);
        if (!r().f5708y) {
            this.Z = 0;
        }
        g.h.a.i.V3(this.f5594f, String.format(Locale.US, "mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(this.Z), Integer.valueOf(this.Z)));
        if (this.Z == 0) {
            U((byte) 0);
        }
        if (this.Z >= s().f5710f) {
            g.h.a.i.x0(this.f5594f, "Last send reach the bottom");
        } else {
            B0(s().f5718t);
        }
        s().q();
        x();
        this.J += s().f5711m;
        if (s().p()) {
            g.h.a.i.w0("no pendding image file to upload,");
            try {
                W(s().f5718t, (byte) 1);
                s().f5721w = this.J;
                return 3;
            } catch (g.i.a.c.a unused) {
                s().f5716r = s().f5715q;
                return 1;
            }
        }
        g.h.a.i.w0("has pendding image file to upload");
        if (t().M != 1) {
            if (t().M == 3) {
                g.i.a.c.l.e.a aVar = this.H;
                if (aVar == null) {
                    try {
                        W(s().f5718t, (byte) 1);
                    } catch (g.i.a.c.a unused2) {
                        s().f5716r = s().f5715q;
                        return 1;
                    }
                } else if (Z(aVar, this.J, t().C * 4096)) {
                    try {
                        W(s().f5718t, (byte) 1);
                        g.h.a.i.w0("make device to enter the ota advertiser mode, and let the app continue update image");
                        this.f5602t = true;
                        this.J = 0;
                        V((byte) 1, true);
                        gVarS = s();
                    } catch (g.i.a.c.a unused3) {
                        s().f5716r = s().f5715q;
                        return 1;
                    }
                } else {
                    try {
                        W(s().f5718t, (byte) 0);
                    } catch (g.i.a.c.a unused4) {
                        s().f5716r = s().f5715q;
                        return 1;
                    }
                }
            } else {
                try {
                    W(s().f5718t, (byte) 0);
                } catch (g.i.a.c.a unused5) {
                    s().f5716r = s().f5715q;
                    return 1;
                }
            }
            return 2;
        }
        try {
            W(s().f5718t, (byte) 1);
            this.O = this.P;
            this.f5602t = true;
            this.J = 0;
            Y(true);
            gVarS = s();
        } catch (g.i.a.c.a unused6) {
            s().f5716r = s().f5715q;
            return 1;
        }
        int i2 = gVarS.f5721w / 30;
        c(30000L);
        return 2;
    }

    public final void y0() throws g.i.a.c.g.c {
        String string;
        if (t().f5675k <= 5) {
            Y(false);
        } else {
            j0(0);
            if (t().f5687w == 1) {
                this.w0 = 2;
            } else {
                int i2 = t().f5687w;
                this.w0 = 1;
            }
        }
        z(522, true);
        try {
            g.h.a.i.x0(false, "wait master to handover ...");
            Thread.sleep(((long) r().f5692b0) * 1000);
        } catch (InterruptedException unused) {
        }
        boolean z2 = false;
        for (int i3 = 0; i3 < 3; i3++) {
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
            if (T(this.O, 1) == 0) {
                this.W = new g.i.a.c.q.i(17, 2);
                r0();
                if (t().f5675k >= 6) {
                    z2 = t().f5687w == this.w0;
                    if (z2) {
                        StringBuilder sbF = g.a.a.a.a.F("role swap success, bud role=");
                        sbF.append(t().f5687w);
                        string = sbF.toString();
                        g.h.a.i.x0(false, string);
                        break;
                    }
                    g.h.a.i.x0(this.f5594f, String.format(Locale.US, "rws swap not completed, hfpState=0x%02X,bud role(%d), target is %d", Integer.valueOf(g.i.a.b.e.b.a.e(1, G(this.P))), Integer.valueOf(t().f5687w), Integer.valueOf(this.w0)));
                } else {
                    t0();
                    z2 = t().f5688x == 0;
                    if (z2) {
                        string = String.format("role swap success, RwsUpdateFlag = 0x%02X", Integer.valueOf(t().f5688x));
                        g.h.a.i.x0(false, string);
                        break;
                    }
                    g.h.a.i.x0(this.f5594f, String.format("rws state not read, RwsUpdateFlag = 0x%02X", Integer.valueOf(t().f5688x)));
                }
            }
        }
        if (!z2) {
            throw new g.i.a.c.g.c("role swap failed", DfuBaseService.NOTIFICATION_ID);
        }
    }

    public final boolean z0(int i2) {
        z(521, true);
        s().s();
        if (this.f5594f) {
            g.h.a.i.U3(String.format("forceCopyProcedure, imageId=0x%04X", Integer.valueOf(i2)));
            g.h.a.i.U3(s().toString());
        }
        try {
            f0(i2);
            s().q();
            x();
            return true;
        } catch (g.i.a.c.a unused) {
            return false;
        }
    }
}
