package g.i.a.c.v;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import com.cvte.vman.instance.InvokeCmd;
import g.i.a.c.l.c;
import g.i.a.c.q.i;
import io.netty.handler.codec.compression.Bzip2Constants;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import no.nordicsemi.android.dfu.DfuServiceInitiator;

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

    public final boolean A0() throws g.i.a.c.g.b, g.i.a.c.g.c {
        String str;
        boolean z2;
        String str2 = 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 iD0 = D0(str2, r().M);
        if (iD0 == 0) {
            str = "user aborted";
        } else {
            if (iD0 == 4128) {
                throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iD0);
            }
            ArrayList arrayList = new ArrayList();
            if (str2 != null && !BluetoothAdapter.checkBluetoothAddress(str2)) {
                throw new IllegalArgumentException(g.a.a.a.a.t("invalid device address ", str2));
            }
            str = "user aborted";
            arrayList.add(new g.i.a.b.e.h.g.a(null, str2, null, null, null, null, null, null, null, -1, null, null, 0, null));
            g.i.a.b.e.h.e eVarA0 = a0();
            eVarA0.A = arrayList;
            eVarA0.f5392q = str2;
            F(eVarA0);
            int iD02 = D0(str2, r().M);
            if (iD02 != 0) {
                if (iD02 == 4128) {
                    throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iD02);
                }
                throw new g.i.a.c.g.c("connectRemoteDevice failed", iD02);
            }
        }
        f0();
        if (this.f5601s) {
            z2 = true;
            J();
        } else {
            this.f5601s = false;
            z2 = true;
            z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_GET, true);
            try {
                Thread.sleep(1500L);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
            this.W = new i(this.f5596n, 2);
            if (this.f5600r) {
                throw new g.i.a.c.g.c(str, InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
            }
            g.h.a.i.w0(String.format(Locale.US, "<< OPCODE_DFU_DEVICE_INFO (0x%02X)", (byte) 13));
            R(this.F0, new byte[]{13}, false);
            if (this.f5594f) {
                g.h.a.i.w0("... waiting OPCODE_DFU_DEVICE_INFO response");
            }
            byte[] bArrC0 = c0();
            if (bArrC0[2] != 1) {
                throw g.a.a.a.a.x0("Get dev info failed", "get remote dev info failed", 270);
            }
            t().s(bArrC0, 3);
            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) {
                w0();
            }
            v0();
            if (this.f5594f) {
                g.h.a.i.w0(t().toString());
            }
            K();
            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 i2 = it.next().f5573x;
                }
            }
            this.f5601s = true;
            g.h.a.i.w0("Ota Environment prepared.");
        }
        if (this.G != null) {
            return z2;
        }
        j0(4097, false);
        return false;
    }

    public final boolean B0() {
        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 = true;
        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) {
                        z0();
                    }
                    j0(i3, false);
                }
            }
            if (!A0() || !C0()) {
                return false;
            }
            this.J += s().f5711m;
            if (s().p()) {
                g.h.a.i.w0("no pendding image file to upload.");
                s().f5721w = this.J;
                if (this.T) {
                    t0();
                    s0();
                    i2 = Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
                } else {
                    i2 = 523;
                }
                z(i2, true);
                z2 = true;
            } else {
                g.h.a.i.U3("has pendding image file to upload");
                if (t().M == 1) {
                    this.O = this.P;
                    this.f5602t = true;
                    this.J = 0;
                    this.f5601s = false;
                    t0();
                    s0();
                } else if (t().M == 3 && (aVar = this.H) != null && n0(aVar, this.J, t().C * 4096)) {
                    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;
                    this.f5601s = false;
                    t0();
                    h0((byte) 1);
                }
                c(DfuServiceInitiator.DEFAULT_SCAN_TIMEOUT);
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e3) {
                e3.printStackTrace();
            }
            if (z2) {
                return z2;
            }
        }
        j0(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, true);
        return false;
    }

    /* JADX WARN: Code restructure failed: missing block: B:94:0x0289, code lost:
    
        r0 = r10;
        r1 = r11;
        r2 = r12;
        r4 = r13;
        r12 = r15;
        r15 = r20;
        r13 = r21;
     */
    /* JADX WARN: Removed duplicated region for block: B:163:0x0417  */
    /* JADX WARN: Removed duplicated region for block: B:164:0x0421  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean C0() throws g.i.a.c.g.b, g.i.a.c.g.c {
        /*
            Method dump skipped, instruction units count: 1098
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.v.f.C0():boolean");
    }

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

    public final void E0(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, g.i.a.c.l.e.a aVar) throws g.i.a.c.g.c {
        int iG;
        g.h.a.i.U3("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) {
                g.h.a.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) {
                    g.h.a.i.U3("reach the end of the file, only read some");
                    iG = s().d();
                }
                int i4 = iG;
                if (i4 <= 0) {
                    if (s().o()) {
                        g.h.a.i.V1("image file has already been send over");
                        return;
                    }
                    g.h.a.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;
                            }
                        }
                    }
                }
                Q(bluetoothGatt, bluetoothGattCharacteristic, bArr, i4, false);
                I();
                i();
            } catch (IOException unused) {
                throw new g.i.a.c.g.c("Error while reading file", 257);
            }
        }
    }

    public final void F0(int i2) throws g.i.a.c.g.b {
        String str;
        int i3 = this.Z;
        if (i3 == 0) {
            this.Z = 12;
            if (this.f5594f) {
                str = String.format(Locale.US, "First Packet, mImageUpdateOffset=0x%08X(%d)", 12, Integer.valueOf(this.Z));
                g.h.a.i.U3(str);
            }
        } else if (this.f5594f) {
            str = String.format(Locale.US, "mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(i3), Integer.valueOf(this.Z));
            g.h.a.i.U3(str);
        }
        i0(i2, this.Z);
        int i4 = s().f5711m;
        int i5 = this.Z;
        if (i4 == i5 || i5 == -1) {
            return;
        }
        g.h.a.i.w0("mBytesSent != mImageUpdateOffset, reload image bin file");
        K();
        g(this.Z, false);
    }

    @Override // 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.w0("invalid FileIndex: " + i3 + ", reset to 0");
            i3 = 0;
        }
        s().f5714p = i3;
        Iterator<g.i.a.c.l.e.a> it = this.F.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            g.i.a.c.l.e.a next = it.next();
            if (g.i.a.c.a0.d.a(this.l0, next.f5570u) >= 0) {
                g.h.a.i.w0(String.format("ignore 0x%04X, alreay ota", Integer.valueOf(next.f5570u)));
            } else if (next.f5570u == t().W) {
                g.h.a.i.w0(String.format("find NoTempImageId: 0x%04X", Integer.valueOf(next.f5570u)));
                this.G = next;
                break;
            }
        }
        g.i.a.c.l.e.a aVar = this.G;
        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(), r().f5707x);
        } else {
            g.h.a.i.V3(false, "mCurBinInputStream == null");
        }
        this.H = null;
        this.I = -1;
    }

    @Override // g.i.a.c.o.b
    public void K() throws g.i.a.c.g.b {
        k(this.G);
        c.b bVar = new c.b();
        bVar.f5536e = r().f5698o;
        bVar.f5533b = r().f5700q;
        bVar.f5534c = this.Q;
        bVar.f5535d = r().c();
        bVar.a = this.f5595m;
        bVar.f5541j = this.R;
        bVar.f5547p = r().f5697n;
        bVar.f5542k = t();
        bVar.f5545n = r().n();
        bVar.f5546o = r().o();
        boolean zP = r().p();
        int i2 = r().f5705v;
        bVar.f5543l = zP;
        bVar.f5544m = i2;
        List<g.i.a.c.l.e.a> listO = g.i.a.c.l.b.o(bVar.a());
        this.F = new ArrayList();
        if (listO != null && listO.size() > 0) {
            Iterator<g.i.a.c.l.e.a> it = listO.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                g.i.a.c.l.e.a next = it.next();
                if (next.f5570u == t().W) {
                    this.F.add(next);
                    break;
                }
            }
        }
        List<g.i.a.c.l.e.a> list = this.F;
        if (list == null || list.size() <= 0) {
            g.h.a.i.W3("pendingImageInputStreams == null || pendingImageInputStreams.size() <= 0");
            throw new g.i.a.c.g.b("no available file to update", 4097);
        }
        if (s().f5716r == 0) {
            this.l0 = new int[this.F.size()];
        }
        s().f5713o = this.F.size();
        if (this.f5594f) {
            g.h.a.i.U3(s().toString());
        }
        J();
    }

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

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