package g.i.a.c.w;

import android.content.Context;
import android.text.TextUtils;
import com.cvte.vman.instance.InvokeCmd;
import g.h.a.i;
import io.netty.handler.codec.compression.Bzip2Constants;
import java.util.ArrayList;
import no.nordicsemi.android.dfu.DfuServiceInitiator;

/* JADX INFO: loaded from: classes.dex */
public class d extends c {
    public d(Context context, g.i.a.c.q.f fVar, g.i.a.c.m.a.b bVar) {
        super(context, fVar, bVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:44:0x0178  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x0197  */
    /* JADX WARN: Removed duplicated region for block: B:50:0x01a6  */
    /* JADX WARN: Removed duplicated region for block: B:51:0x01ac  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean A0() throws g.i.a.c.g.c {
        /*
            Method dump skipped, instruction units count: 499
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.w.d.A0():boolean");
    }

    public final void B0(String str) throws g.i.a.c.g.c {
        if (this.f5600r) {
            throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
        }
        z(520, true);
        int iG0 = g0(str, r().M);
        if (iG0 == 0) {
            return;
        }
        if (iG0 != 4128) {
            throw new g.i.a.c.g.c("connectOtaRemoteDevice failed", iG0);
        }
        throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iG0);
    }

    @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 iG0 = g0(this.O, r().M);
            if (iG0 != 0) {
                i.W3(String.format("Something error in OTA process, errorCode: 0x%04X, mProcessState=0x%04X", Integer.valueOf(iG0), Integer.valueOf(this.E)));
                j0(iG0, true);
                return false;
            }
        }
        if (z2) {
            try {
                q0();
                z(Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE, true);
            } catch (g.i.a.c.a e2) {
                i.W3(e2.toString());
                j0(e2.f5446m, false);
            }
        } else {
            if (w0()) {
                D();
            }
            j0(274, false);
        }
        return true;
    }

    @Override // g.i.a.c.m.a.a
    public void o() {
        int iV;
        try {
            setName("ProcessorXG0010N");
            i.w0("ProcessorXG0010N running.");
            iV = v();
        } catch (Exception e2) {
            i.W3(e2.toString());
            j0(0, false);
        }
        if (iV != 0) {
            j0(iV, false);
            return;
        }
        z(InvokeCmd.CMD_XBUG_GET_VMAN_SERVICE_LOG_LEVEL, true);
        this.O = this.P;
        this.f5602t = false;
        y0();
        k(this.G);
        i.x0(this.f5594f, "ProcessorXG0010N stopped");
        if (this.E == 525) {
            z(259, true);
        }
    }

    public final boolean x0() throws g.i.a.c.g.b, g.i.a.c.g.c {
        int i2;
        boolean zR;
        if (this.f5602t) {
            if (TextUtils.isEmpty(this.O)) {
                ArrayList arrayList = new ArrayList();
                if (t().f5674j == 5 || t().f5674j == 9 || t().f5674j == 4 || t().f5674j == 6 || t().f5674j == 7 || t().f5674j == 8 || t().f5674j == 10 || t().f5674j == 11 || t().f5674j == 12) {
                    int i3 = r().Q;
                    byte[] bArr = t().D;
                    if (bArr != null && i3 < 0) {
                        throw new IllegalArgumentException("invalid manufacture id");
                    }
                    arrayList.add(new g.i.a.b.e.h.g.a(null, null, null, null, null, null, null, null, null, i3, bArr, null, 0, null));
                } else {
                    int i4 = r().Q;
                    byte[] bArrL = g.i.a.c.m.a.a.l(this.P);
                    if (i4 < 0) {
                        throw new IllegalArgumentException("invalid manufacture id");
                    }
                    arrayList.add(new g.i.a.b.e.h.g.a(null, null, null, null, null, null, null, null, null, i4, bArrL, null, 0, null));
                }
                g.i.a.b.e.h.e eVarA0 = a0();
                eVarA0.A = arrayList;
                O(eVarA0, 31000L);
            }
            B0(this.O);
            if (this.f5601s) {
                J();
            } else {
                z0();
            }
            if (this.G == null) {
                j0(4097, false);
                return false;
            }
        } else {
            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 iG0 = g0(str, r().M);
            if (iG0 != 0) {
                if (iG0 == 4128) {
                    throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iG0);
                }
                F(a0());
                if (this.f5600r) {
                    throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
                }
                int iG02 = g0(str, r().M);
                if (iG02 != 0) {
                    if (iG02 == 4128) {
                        throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iG02);
                    }
                    throw new g.i.a.c.g.c("connectRemoteDevice failed", iG02);
                }
            }
            if (this.y0 == null) {
                g.a.a.a.a.u0(this.x0, g.a.a.a.a.F("OTA SERVICE not found:"));
                i2 = 262;
            } else if (this.z0 == null) {
                g.a.a.a.a.u0(g.a, g.a.a.a.a.F("not found OTA_CONTROL_ENTER_OTA_MODE_CHARACTERISTIC : "));
                i2 = 263;
            } else {
                i2 = 0;
            }
            if (i2 != 0) {
                throw new g.i.a.c.g.c("load ota service failed", i2);
            }
            if (this.f5601s) {
                J();
            } else {
                z0();
            }
            if (this.G == null) {
                j0(4097, false);
                return false;
            }
            if (this.f5600r) {
                throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
            }
            if (this.z0 == null) {
                zR = false;
            } else {
                z(InvokeCmd.CMD_XBUG_GET_VMAN_FUNTION_TIME_OUT_LIST, true);
                i.w0("<< OPCODE_ENTER_OTA_MODE(0x01), enable device to enter OTA mode");
                try {
                    zR = R(this.z0, g.f5763d, false);
                } catch (g.i.a.c.a e2) {
                    boolean z2 = e2.f5446m != 267;
                    StringBuilder sbF = g.a.a.a.a.F("<< OPCODE_ENTER_OTA_MODE(0x01) failed, ignore it :");
                    sbF.append(e2.getMessage());
                    i.W3(sbF.toString());
                    this.M = 0;
                    zR = z2;
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException unused) {
                }
                if (zR && r().f5693c0) {
                    D();
                }
                L(this.q0);
            }
            if (!zR) {
                j0(280, false);
                return false;
            }
            ArrayList arrayList2 = new ArrayList();
            if (t().f5674j == 5 || t().f5674j == 9 || t().f5674j == 4 || t().f5674j == 6 || t().f5674j == 7 || t().f5674j == 8 || t().f5674j == 11 || t().f5674j == 12) {
                int i5 = r().Q;
                byte[] bArr2 = t().D;
                if (bArr2 != null && i5 < 0) {
                    throw new IllegalArgumentException("invalid manufacture id");
                }
                arrayList2.add(new g.i.a.b.e.h.g.a(null, null, null, null, null, null, null, null, null, i5, bArr2, null, 0, null));
            } else {
                int i6 = r().Q;
                byte[] bArrL2 = g.i.a.c.m.a.a.l(this.P);
                if (i6 < 0) {
                    throw new IllegalArgumentException("invalid manufacture id");
                }
                arrayList2.add(new g.i.a.b.e.h.g.a(null, null, null, null, null, null, null, null, null, i6, bArrL2, null, 0, null));
            }
            g.i.a.b.e.h.e eVarA02 = a0();
            eVarA02.A = arrayList2;
            O(eVarA02, 31000L);
            B0(this.O);
        }
        f0();
        this.f5602t = true;
        return true;
    }

    public final boolean y0() {
        g.i.a.c.l.e.a aVar;
        int i2;
        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) {
                    j0(i3, true);
                } else {
                    if (i3 != 4097 && i3 != 265 && w0() && this.S == 0) {
                        D();
                    }
                    j0(i3, false);
                }
            }
            if (!x0() || !A0()) {
                return false;
            }
            this.J += s().f5711m;
            if (s().p()) {
                i.w0("no pendding image file to upload.");
                i0(s().f5718t, (byte) 1);
                s().f5721w = this.J;
                if (this.T) {
                    q0();
                    i2 = Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
                } else {
                    i2 = 523;
                }
                z(i2, true);
                z2 = true;
            } else {
                i.x0(this.f5594f, "has pendding image file to upload");
                if (t().M == 1) {
                    i0(s().f5718t, (byte) 1);
                    this.O = this.P;
                    this.f5602t = false;
                    this.J = 0;
                    q0();
                } else if (t().M == 3 && (aVar = this.H) != null && m0(aVar, this.J, t().C * 4096)) {
                    i.w0("make device to enter the ota advertiser mode, and let the app continue update image");
                    i0(s().f5718t, (byte) 1);
                    this.O = null;
                    this.f5602t = true;
                    this.J = 0;
                    h0((byte) 1);
                } else {
                    g.i.a.c.q.g gVarS = s();
                    i0(gVarS.f5718t, (byte) 0);
                }
                c(DfuServiceInitiator.DEFAULT_SCAN_TIMEOUT);
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException unused) {
            }
            if (z2) {
                return z2;
            }
        }
        j0(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, true);
        return false;
    }

    public final void z0() throws g.i.a.c.g.b {
        this.f5601s = false;
        z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_GET, true);
        try {
            Thread.sleep(1500L);
        } catch (InterruptedException unused) {
        }
        this.W = new g.i.a.c.q.i(16, 2);
        s0();
        t0();
        r0();
        u0();
        if (this.f5594f) {
            i.w0(t().toString());
        }
        K();
        this.f5601s = true;
        i.w0("Ota Environment prepared.");
    }
}
