package g.i.a.c.s;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import com.cvte.vman.instance.InvokeCmd;
import g.h.a.i;
import g.i.a.c.p.f;
import io.netty.handler.codec.dns.DnsRecord;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;

/* JADX INFO: loaded from: classes.dex */
public abstract class b extends g.i.a.c.p.c {
    public BluetoothGattCharacteristic H0;
    public BluetoothGattCharacteristic I0;
    public BluetoothGattCharacteristic J0;
    public BluetoothGattCharacteristic K0;
    public List<BluetoothGattCharacteristic> L0;
    public final BluetoothGattCallback M0;

    public class a extends BluetoothGattCallback {
        public a() {
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            byte[] value = bluetoothGattCharacteristic.getValue();
            synchronized (this) {
                if (value == null) {
                    i.W3("notification data invalid");
                } else if (value.length < 2) {
                    i.W3("notification data invalid");
                } else {
                    int i2 = value[0] & 255;
                    int i3 = value[1] & 255;
                    Objects.requireNonNull(b.this);
                    if (i2 == 16) {
                        if (i3 == 7) {
                            synchronized (b.this.V) {
                                i.U3("ignore connection parameters notification");
                                b.this.r0 = value;
                                b.this.t0 = true;
                                b.this.V.notifyAll();
                            }
                        } else if (i3 != 8) {
                            synchronized (b.this.V) {
                                b.this.r0 = value;
                                b.this.t0 = true;
                                b.this.V.notifyAll();
                            }
                        } else {
                            byte b2 = value.length >= 3 ? value[2] : (byte) 0;
                            i.U3("remote state changed, busyMode=" + ((int) b2));
                            synchronized (b.this.e0) {
                                b bVar = b.this;
                                bVar.f5593d0 = b2 == 1;
                                bVar.e0.notifyAll();
                            }
                        }
                    }
                }
            }
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) {
            b bVar = b.this;
            if (i2 == 0) {
                bVar.f5608z = bluetoothGattCharacteristic.getValue();
            } else {
                bVar.M = i2 | 1024;
                i.D0(String.format(Locale.US, "read Characteristic error:0x%04X", Integer.valueOf(b.this.M)));
            }
            b.this.y();
        }

        /* JADX WARN: Removed duplicated region for block: B:10:0x002f  */
        /* JADX WARN: Removed duplicated region for block: B:9:0x001f  */
        @Override // android.bluetooth.BluetoothGattCallback
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public void onCharacteristicWrite(android.bluetooth.BluetoothGatt r6, android.bluetooth.BluetoothGattCharacteristic r7, int r8) {
            /*
                r5 = this;
                byte[] r6 = r7.getValue()
                java.lang.String r0 = "characteristic'value is null, exception"
                r1 = 0
                if (r8 != 0) goto L33
                g.i.a.c.s.b r8 = g.i.a.c.s.b.this
                r8.C = r1
                g.i.a.c.s.b r8 = g.i.a.c.s.b.this
                java.util.UUID r8 = r8.C0
                if (r8 == 0) goto L95
                java.util.UUID r7 = r7.getUuid()
                boolean r7 = r8.equals(r7)
                if (r7 == 0) goto L95
                if (r6 == 0) goto L2f
            L1f:
                g.i.a.c.s.b r7 = g.i.a.c.s.b.this
                g.i.a.c.q.g r7 = r7.s()
                int r6 = r6.length
                r7.a(r6)
                g.i.a.c.s.b r6 = g.i.a.c.s.b.this
                r6.x()
                goto L95
            L2f:
                g.h.a.i.W3(r0)
                goto L95
            L33:
                r2 = 257(0x101, float:3.6E-43)
                r3 = 1
                r4 = 143(0x8f, float:2.0E-43)
                if (r8 == r2) goto L59
                if (r8 != r4) goto L3d
                goto L59
            L3d:
                g.i.a.c.s.b r6 = g.i.a.c.s.b.this
                r7 = r8 | 1024(0x400, float:1.435E-42)
                r6.M = r7
                java.lang.Object[] r6 = new java.lang.Object[r3]
                g.i.a.c.s.b r7 = g.i.a.c.s.b.this
                int r7 = r7.M
                java.lang.Integer r7 = java.lang.Integer.valueOf(r7)
                r6[r1] = r7
                java.lang.String r7 = "Characteristic write error: 0x%04X"
                java.lang.String r6 = java.lang.String.format(r7, r6)
                g.h.a.i.W3(r6)
                goto L95
            L59:
                g.i.a.c.s.b r2 = g.i.a.c.s.b.this
                java.util.UUID r2 = r2.C0
                if (r2 == 0) goto L95
                java.util.UUID r7 = r7.getUuid()
                boolean r7 = r2.equals(r7)
                if (r7 == 0) goto L95
                if (r8 != r4) goto L72
                g.i.a.c.s.b r7 = g.i.a.c.s.b.this
                r7.C = r1
                if (r6 == 0) goto L2f
                goto L1f
            L72:
                g.i.a.c.s.b r6 = g.i.a.c.s.b.this
                r6.C = r3
                g.i.a.c.s.b r6 = g.i.a.c.s.b.this
                boolean r6 = r6.f5594f
                if (r6 == 0) goto L95
                java.lang.StringBuilder r6 = new java.lang.StringBuilder
                r6.<init>()
                java.lang.String r7 = "write image packet error, status="
                r6.append(r7)
                r6.append(r8)
                java.lang.String r7 = ", please retry."
                r6.append(r7)
                java.lang.String r6 = r6.toString()
                g.h.a.i.w0(r6)
            L95:
                g.i.a.c.s.b r6 = g.i.a.c.s.b.this
                r6.q()
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.s.b.a.onCharacteristicWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int):void");
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i2, int i3) {
            if (i2 != 0) {
                if (i3 == 0) {
                    b.this.B(0);
                }
                b.this.M = i2 | 2048;
            } else if (i3 == 2) {
                b bVar = b.this;
                if (bVar.f5600r) {
                    i.W3("task already aborted, ignore");
                    return;
                } else if (bVar.f5605w == 256) {
                    g.i.a.b.e.g.a.b(bluetoothGatt);
                    b.this.b0();
                    return;
                }
            } else if (i3 == 0) {
                if (b.this.E == 521) {
                    b.this.M = i2 | 2048;
                    b bVar2 = b.this;
                    if (bVar2.f5594f) {
                        i.w0(String.format("disconnect in OTA process, mErrorState: 0x%04X", Integer.valueOf(bVar2.M)));
                    }
                    b.this.q();
                }
                b.this.B(0);
            }
            b.this.w();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i2) {
            if (i2 != 0) {
                b.this.M = i2 | 1024;
            } else if (g.i.a.c.p.f.f5633b.equals(bluetoothGattDescriptor.getUuid())) {
                b.this.s0 = true;
            }
            b.this.n();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onMtuChanged(BluetoothGatt bluetoothGatt, int i2, int i3) {
            if (i3 == 0 && b.this.r().F) {
                b.this.W(i2);
            }
            b.this.u0 = true;
            b.this.n();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onPhyUpdate(BluetoothGatt bluetoothGatt, int i2, int i3, int i4) {
            super.onPhyUpdate(bluetoothGatt, i2, i3, i4);
            b bVar = b.this;
            Objects.requireNonNull(bVar);
            i.V3(false, String.format("onPhyUpdate: mConnectionState=0x%04X", Integer.valueOf(bVar.f5605w)));
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i2) {
            UUID uuidA;
            BluetoothGattCharacteristic characteristic;
            StringBuilder sbF;
            UUID uuid;
            StringBuilder sbF2;
            String str;
            b bVar = b.this;
            if (bVar.f5600r) {
                i.W3("task already aborted, ignore");
                return;
            }
            if (i2 == 0) {
                try {
                    bVar.x0 = UUID.fromString(bVar.r().R);
                    b bVar2 = b.this;
                    bVar2.B0 = UUID.fromString(bVar2.r().S);
                    b bVar3 = b.this;
                    bVar3.C0 = UUID.fromString(bVar3.r().T);
                    b bVar4 = b.this;
                    bVar4.D0 = UUID.fromString(bVar4.r().U);
                } catch (Exception e2) {
                    e2.printStackTrace();
                    i.D0(e2.toString());
                }
                b bVar5 = b.this;
                BluetoothGattService service = bluetoothGatt.getService(bVar5.x0);
                bVar5.y0 = service;
                if (service == null) {
                    g.a.a.a.a.u0(bVar5.x0, g.a.a.a.a.F("OTA_SERVICE not found: "));
                } else {
                    if (bVar5.f5594f) {
                        g.a.a.a.a.t0(bVar5.x0, g.a.a.a.a.F("find OTA_SERVICE: "));
                    }
                    BluetoothGattService bluetoothGattService = bVar5.y0;
                    UUID uuid2 = g.a;
                    BluetoothGattCharacteristic characteristic2 = bluetoothGattService.getCharacteristic(uuid2);
                    bVar5.z0 = characteristic2;
                    if (characteristic2 == null && bVar5.S == 0) {
                        g.a.a.a.a.j0("OTA_CONTROL_ENTER_OTA_MODE_CHARACTERISTIC not found: ", uuid2);
                    } else if (bVar5.f5594f) {
                        g.a.a.a.a.i0("find OTA_CONTROL_ENTER_OTA_MODE_CHARACTERISTIC = ", uuid2);
                        g.a.a.a.a.V(bVar5.z0);
                    }
                    BluetoothGattCharacteristic bluetoothGattCharacteristic = bVar5.z0;
                    if (bluetoothGattCharacteristic != null) {
                        bluetoothGattCharacteristic.setWriteType(1);
                    }
                    BluetoothGattService bluetoothGattService2 = bVar5.y0;
                    UUID uuid3 = g.f5742b;
                    BluetoothGattCharacteristic characteristic3 = bluetoothGattService2.getCharacteristic(uuid3);
                    bVar5.H0 = characteristic3;
                    if (characteristic3 == null) {
                        g.a.a.a.a.j0("OTA_MAC_ADDR_CHARACTERISTIC_UUID not found:", uuid3);
                    } else if (bVar5.f5594f) {
                        g.a.a.a.a.i0("find OTA_MAC_ADDR_CHARACTERISTIC_UUID = ", uuid3);
                        g.a.a.a.a.V(bVar5.z0);
                    }
                    BluetoothGattService bluetoothGattService3 = bVar5.y0;
                    UUID uuid4 = g.f5743c;
                    BluetoothGattCharacteristic characteristic4 = bluetoothGattService3.getCharacteristic(uuid4);
                    bVar5.J0 = characteristic4;
                    if (characteristic4 == null) {
                        g.a.a.a.a.i0("OTA_READ_PATCH_CHARACTERISTIC_UUID not found:", uuid4);
                    } else if (bVar5.f5594f) {
                        g.a.a.a.a.t0(uuid4, g.a.a.a.a.F("find OTA_PATCH_VERSION_CHARACTERISTIC_UUID: "));
                        g.a.a.a.a.V(bVar5.J0);
                    }
                    BluetoothGattService bluetoothGattService4 = bVar5.y0;
                    UUID uuid5 = g.f5744d;
                    BluetoothGattCharacteristic characteristic5 = bluetoothGattService4.getCharacteristic(uuid5);
                    bVar5.I0 = characteristic5;
                    if (characteristic5 == null) {
                        g.a.a.a.a.j0("OTA_READ_APP_CHARACTERISTIC_UUID not found: ", uuid5);
                    } else if (bVar5.f5594f) {
                        g.a.a.a.a.t0(uuid5, g.a.a.a.a.F("find OTA_APP_VERSION_CHARACTERISTIC_UUID: "));
                        g.a.a.a.a.V(bVar5.I0);
                    }
                    BluetoothGattService bluetoothGattService5 = bVar5.y0;
                    UUID uuid6 = g.f5745e;
                    BluetoothGattCharacteristic characteristic6 = bluetoothGattService5.getCharacteristic(uuid6);
                    bVar5.K0 = characteristic6;
                    if (characteristic6 == null) {
                        g.a.a.a.a.j0("OTA_PATCH_EXTENSION_VERSION_CHARACTERISTIC_UUID not found:", uuid6);
                    } else if (bVar5.f5594f) {
                        g.a.a.a.a.t0(uuid6, g.a.a.a.a.F("find OTA_PATCH_EXTENSION_VERSION_CHARACTERISTIC_UUID: "));
                        g.a.a.a.a.V(bVar5.K0);
                    }
                    BluetoothGattService bluetoothGattService6 = bVar5.y0;
                    UUID uuid7 = g.f5746f;
                    BluetoothGattCharacteristic characteristic7 = bluetoothGattService6.getCharacteristic(uuid7);
                    bVar5.A0 = characteristic7;
                    if (characteristic7 == null) {
                        g.a.a.a.a.j0("OTA_DEVICE_INFO_CHARACTERISTIC_UUID not found:", uuid7);
                    } else if (bVar5.f5594f) {
                        g.a.a.a.a.i0("find OTA_DEVICE_INFO_CHARACTERISTIC_UUID: ", uuid7);
                        g.a.a.a.a.V(bVar5.A0);
                    }
                    bVar5.L0 = new ArrayList();
                    for (int i3 = 65504; i3 < 65519 && (characteristic = bVar5.y0.getCharacteristic((uuidA = g.i.a.b.e.i.a.a(i3)))) != null; i3++) {
                        if (bVar5.f5594f) {
                            StringBuilder sbF3 = g.a.a.a.a.F("find image version characteristic: ");
                            sbF3.append(uuidA.toString());
                            i.U3(sbF3.toString());
                        }
                        g.a.a.a.a.U(characteristic);
                        bVar5.L0.add(characteristic);
                    }
                }
                b bVar6 = b.this;
                BluetoothGattService service2 = bluetoothGatt.getService(bVar6.B0);
                bVar6.E0 = service2;
                if (service2 == null) {
                    StringBuilder sbF4 = g.a.a.a.a.F("DFU_SERVICE not found:");
                    sbF4.append(bVar6.B0);
                    i.W3(sbF4.toString());
                } else {
                    if (bVar6.f5594f) {
                        g.a.a.a.a.t0(bVar6.B0, g.a.a.a.a.F("find DFU_SERVICE: "));
                    }
                    BluetoothGattCharacteristic characteristic8 = bVar6.E0.getCharacteristic(bVar6.D0);
                    bVar6.F0 = characteristic8;
                    if (characteristic8 == null) {
                        sbF = g.a.a.a.a.F("not found DFU_CONTROL_POINT_UUID: ");
                        uuid = bVar6.D0;
                    } else {
                        if (bVar6.f5594f) {
                            g.a.a.a.a.t0(bVar6.D0, g.a.a.a.a.F("find DFU_CONTROL_POINT_UUID: "));
                        }
                        bVar6.F0.setWriteType(2);
                        g.a.a.a.a.V(bVar6.F0);
                        BluetoothGattCharacteristic characteristic9 = bVar6.E0.getCharacteristic(bVar6.C0);
                        bVar6.G0 = characteristic9;
                        if (characteristic9 == null) {
                            sbF = g.a.a.a.a.F("not found DFU_DATA_UUID: ");
                            uuid = bVar6.C0;
                        } else {
                            if (bVar6.f5594f) {
                                g.a.a.a.a.t0(bVar6.C0, g.a.a.a.a.F("find DFU_DATA_UUID: "));
                            }
                            bVar6.G0.setWriteType(1);
                            g.a.a.a.a.V(bVar6.G0);
                        }
                    }
                    g.a.a.a.a.t0(uuid, sbF);
                }
                b bVar7 = b.this;
                Objects.requireNonNull(bVar7);
                UUID uuid8 = f.b.a;
                BluetoothGattService service3 = bluetoothGatt.getService(uuid8);
                if (service3 != null) {
                    uuid8 = f.b.f5640b;
                    if (service3.getCharacteristic(uuid8) == null) {
                        sbF2 = new StringBuilder();
                        str = "DIS_PNP_ID_CHARACTERISTIC not found:";
                        sbF2.append(str);
                        sbF2.append(uuid8);
                        i.w0(sbF2.toString());
                    } else if (bVar7.f5594f) {
                        sbF2 = g.a.a.a.a.F("find DIS_PNP_ID_CHARACTERISTIC: ");
                        sbF2.append(uuid8.toString());
                        i.w0(sbF2.toString());
                    }
                } else if (bVar7.f5594f) {
                    sbF2 = new StringBuilder();
                    str = "DEVICE_INFORMATION_SERVICE not found:";
                    sbF2.append(str);
                    sbF2.append(uuid8);
                    i.w0(sbF2.toString());
                }
                b.this.B(InvokeCmd.CMD_XBUG_SET_XBUG_DUMP_CVTE_LOG);
            } else {
                bVar.M = i2 | 2048;
            }
            b.this.w();
        }
    }

    public b(Context context, g.i.a.c.q.f fVar, g.i.a.c.m.a.b bVar) {
        super(context, fVar, bVar);
        this.M0 = new a();
    }

    public void g0(byte b2) {
        j0(new byte[]{4, b2});
    }

    public void h0(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) {
            u0();
        }
        g.i.a.b.e.h.c cVar = this.m0;
        if (cVar != null) {
            cVar.l();
        }
        k(this.G);
        if (r().f(1)) {
            Y(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:0x005f A[Catch: IOException -> 0x0299, TryCatch #0 {IOException -> 0x0299, blocks: (B:13:0x0051, B:15:0x0056, B:18:0x006c, B:20:0x0070, B:22:0x0083, B:24:0x008a, B:26:0x00ab, B:28:0x00af, B:30:0x00b3, B:31:0x00c2, B:33:0x00cc, B:35:0x00d8, B:25:0x0099, B:17:0x005f), top: B:94:0x0051 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void i0(android.bluetooth.BluetoothGatt r19, android.bluetooth.BluetoothGattCharacteristic r20, g.i.a.c.l.e.a r21) {
        /*
            Method dump skipped, instruction units count: 682
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.s.b.i0(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, g.i.a.c.l.e.a):void");
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0034  */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0044  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void j0(byte[] r5) throws g.i.a.c.g.c {
        /*
            r4 = this;
            r0 = 524(0x20c, float:7.34E-43)
            r1 = 1
            r4.z(r0, r1)
            r0 = 0
            java.lang.String r2 = "<< OPCODE_DFU_ACTIVE_IMAGE_RESET(0x04)"
            g.h.a.i.w0(r2)     // Catch: g.i.a.c.a -> L13
            android.bluetooth.BluetoothGattCharacteristic r2 = r4.F0     // Catch: g.i.a.c.a -> L13
            boolean r1 = r4.R(r2, r5, r0)     // Catch: g.i.a.c.a -> L13
            goto L28
        L13:
            r5 = move-exception
            int r2 = r5.f5446m
            r3 = 4128(0x1020, float:5.785E-42)
            if (r2 != r3) goto L1b
            goto L32
        L1b:
            g.i.a.c.q.f r2 = r4.r()
            boolean r2 = r2.f5709z
            if (r2 != 0) goto L2b
            java.lang.String r5 = "active cmd has no response, ignore"
            g.h.a.i.w0(r5)
        L28:
            r3 = r0
            r0 = r1
            goto L32
        L2b:
            java.lang.String r1 = "active cmd has no response, notify error"
            g.h.a.i.W3(r1)
            int r3 = r5.f5446m
        L32:
            if (r0 == 0) goto L44
            java.lang.String r5 = "image active success"
            g.h.a.i.w0(r5)
            int r5 = r4.M
            r4.Y(r5)
            g.i.a.c.l.e.a r5 = r4.G
            r4.k(r5)
            return
        L44:
            g.i.a.c.g.c r5 = new g.i.a.c.g.c
            r5.<init>(r3)
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.s.b.j0(byte[]):void");
    }

    public boolean k0(g.i.a.c.l.e.a aVar, int i2, int i3) {
        i.V3(this.f5594f, String.format(Locale.US, "nextBinSize=%d, mBytesSentBuffer=%d, bufferSize=%d", Integer.valueOf(aVar.h()), Integer.valueOf(i2), Integer.valueOf(i3)));
        return aVar.h() + i2 > i3;
    }

    public void l0(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)");
        R(this.F0, bArr, false);
    }

    public int m0(String str) {
        BluetoothGatt bluetoothGattConnectGatt;
        StringBuilder sb;
        BluetoothDevice bluetoothDeviceG = G(str);
        if (bluetoothDeviceG == null) {
            return 264;
        }
        B(256);
        this.M = 0;
        this.f5603u = false;
        if (this.f5594f) {
            i.U3(String.format("Connecting to device:%s, isConnectedCallbackCome=%b", i.S0(str, true), Boolean.valueOf(this.f5603u)));
        }
        g.i.a.b.e.c cVar = this.p0;
        if (cVar != null) {
            cVar.k(str, this.M0);
            this.p0.d(str, this.M0);
            bluetoothGattConnectGatt = this.p0.f5342g.get(str);
            this.q0 = bluetoothGattConnectGatt;
            try {
                synchronized (this.f5604v) {
                    if (!this.f5603u && this.M == 0) {
                        if (this.f5594f) {
                            i.w0("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 {
            bluetoothGattConnectGatt = bluetoothDeviceG.connectGatt(this.f5595m, false, this.M0);
            this.q0 = bluetoothGattConnectGatt;
            try {
                synchronized (this.f5604v) {
                    if (!this.f5603u && this.M == 0) {
                        if (this.f5594f) {
                            i.w0("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");
                this.M = 260;
            } else if (bluetoothGattConnectGatt == 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());
                this.M = 264;
            } else if (this.f5594f) {
                i.U3("connected the device which going to upgrade");
            }
        }
        return this.M;
    }

    public void n0(int i2) {
        List<g.i.a.c.l.e.a> list = this.F;
        if (list == null) {
            return;
        }
        g.i.a.c.l.e.a aVar = null;
        Iterator<g.i.a.c.l.e.a> it = list.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            g.i.a.c.l.e.a next = it.next();
            if (next.f5570u == i2) {
                aVar = next;
                break;
            }
        }
        if (aVar == null) {
            return;
        }
        int i3 = aVar.f5570u;
        int i4 = aVar.f5572w;
        i.U3(String.format("<< OPCODE_DFU_CHECK_IMAGE(0x%02X)", (byte) 13));
        R(this.F0, new byte[]{13, (byte) (i3 & DnsRecord.CLASS_ANY), (byte) ((i3 >> 8) & DnsRecord.CLASS_ANY), (byte) (i4 & DnsRecord.CLASS_ANY), (byte) ((i4 >> 8) & DnsRecord.CLASS_ANY), (byte) ((i4 >> 16) & DnsRecord.CLASS_ANY), (byte) ((i4 >> 24) & DnsRecord.CLASS_ANY)}, false);
        if (this.f5594f) {
            i.U3("... Reading OPCODE_DFU_CHECK_IMAGE(0x01) notification");
        }
        byte b2 = c0()[2];
        if (b2 != 1) {
            throw g.a.a.a.a.c("0x%02X: check image failed", new Object[]{Byte.valueOf(b2)}, "check image failed", 766);
        }
    }

    public void o0(int i2) {
        int i3;
        int i4;
        if (this.f5594f) {
            i.w0("<< OPCODE_DFU_REPORT_TARGET_IMAGE_INFO(0x06)");
        }
        R(this.F0, new byte[]{6, (byte) (i2 & DnsRecord.CLASS_ANY), (byte) ((i2 >> 8) & DnsRecord.CLASS_ANY)}, false);
        if (this.f5594f) {
            i.U3("... Reading OPCODE_DFU_REPORT_TARGET_IMAGE_INFO(0x06) notification");
        }
        byte[] bArrC0 = c0();
        int length = bArrC0 != null ? bArrC0.length : 0;
        if ((length > 2 ? bArrC0[2] : (byte) -2) != 1) {
            throw g.a.a.a.a.y0("0x%02X, Get target image info failed", new Object[]{766}, "Get target image info failed", 766);
        }
        ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArrC0);
        byteBufferWrap.order(ByteOrder.LITTLE_ENDIAN);
        if (length >= 11) {
            i3 = byteBufferWrap.getShort(3) & 65535;
            i4 = 7;
        } else {
            if (length < 9) {
                this.Z = 0;
                i3 = 0;
                i.w0(String.format(Locale.US, "mOriginalFwVersion=%d, mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(i3), Integer.valueOf(this.Z), Integer.valueOf(this.Z)));
            }
            i3 = byteBufferWrap.getShort(3) & 65535;
            i4 = 5;
        }
        this.Z = byteBufferWrap.getInt(i4);
        i.w0(String.format(Locale.US, "mOriginalFwVersion=%d, mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(i3), Integer.valueOf(this.Z), Integer.valueOf(this.Z)));
    }

    public void p0(int i2) {
        byte[] bArr = {3, (byte) (i2 & DnsRecord.CLASS_ANY), (byte) ((i2 >> 8) & DnsRecord.CLASS_ANY)};
        i.w0("<< OPCODE_DFU_VALIDATE_FW_IMAGE (0x03)");
        R(this.F0, bArr, false);
        int i3 = s().f5710f;
        int iMax = InvokeCmd.CMD_PMSCTRL_SET_PMS_WAKEUP_SOURCES;
        if (i3 > 2097152) {
            iMax = Math.max(((s().f5710f / 1048576) + 1) * 4 * 1000, InvokeCmd.CMD_PMSCTRL_SET_PMS_WAKEUP_SOURCES);
        }
        if (this.f5594f) {
            g.a.a.a.a.a0("... waiting DFU_VALIDATE_FW_IMAGE response for ", iMax);
        }
        byte b2 = V(iMax)[2];
        if (b2 == 1) {
            return;
        }
        if (b2 != 5) {
            throw g.a.a.a.a.y0("0x%02X(not supported), Validate FW failed", new Object[]{Byte.valueOf(b2)}, "Validate FW failed", 766);
        }
        throw g.a.a.a.a.y0("0x%02X, Validate FW failed, CRC check error", new Object[]{Byte.valueOf(b2)}, "Validate FW failed", InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_GET);
    }

    public void q0() {
        j0(new byte[]{4});
    }

    public int r0() {
        if (this.F0 == null) {
            i.W3("no mControlPointCharacteristic found");
            return 0;
        }
        i.w0("<< OPCODE_DFU_ENABLE_BUFFER_CHECK_MODE(0x09)");
        R(this.F0, new byte[]{9}, false);
        try {
            if (this.f5594f) {
                i.w0("... Reading OPCODE_DFU_ENABLE_BUFFER_CHECK_MODE notification");
            }
            byte[] bArrV = V(1600L);
            if (bArrV[2] == 1) {
                ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArrV);
                byteBufferWrap.order(ByteOrder.LITTLE_ENDIAN);
                int i2 = (((short) (byteBufferWrap.get(4) & 255)) << 8) | ((short) (byteBufferWrap.get(3) & 255));
                int i3 = ((short) (byteBufferWrap.get(5) & 255)) | (((short) (byteBufferWrap.get(6) & 255)) << 8);
                if (this.f5594f) {
                    i.U3("maxBufferSize=" + i2 + ", bufferCheckMtuSize=" + i3);
                }
                b(i2);
                T(i3);
                return 1;
            }
        } catch (g.i.a.c.a unused) {
            i.W3("Read DFU_REPORT_OTA_FUNCTION_VERSION failed, just think remote is normal function.");
            this.M = 0;
        }
        return 0;
    }

    public void s0() {
        if (t().f5675k != 0) {
            List<BluetoothGattCharacteristic> list = this.L0;
            byte[] bArr = null;
            if (list == null || list.size() <= 0) {
                g.i.a.c.q.i iVarT = t();
                iVarT.P = null;
                iVarT.i();
                i.w0("no ImageVersionCharacteristics to read");
                return;
            }
            for (BluetoothGattCharacteristic bluetoothGattCharacteristic : this.L0) {
                i.U3("read image version");
                byte[] bArrS = S(bluetoothGattCharacteristic);
                if (bArrS != null) {
                    if (bArr == null) {
                        bArr = bArrS;
                    } else {
                        byte[] bArr2 = new byte[bArr.length + bArrS.length];
                        System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
                        System.arraycopy(bArrS, 0, bArr2, bArr.length, bArrS.length);
                        bArr = bArr2;
                    }
                }
            }
            g.i.a.c.q.i iVarT2 = t();
            iVarT2.P = bArr;
            iVarT2.i();
            return;
        }
        if (this.J0 != null) {
            i.U3("read patch version");
            byte[] bArrS2 = S(this.J0);
            if (bArrS2 != null) {
                try {
                    ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArrS2);
                    byteBufferWrap.order(ByteOrder.LITTLE_ENDIAN);
                    t().E = byteBufferWrap.getInt(0);
                } catch (Exception e2) {
                    i.D0(e2.toString());
                }
            }
        }
        if (this.I0 != null) {
            i.U3("read app version");
            byte[] bArrS3 = S(this.I0);
            if (bArrS3 != null) {
                try {
                    ByteBuffer byteBufferWrap2 = ByteBuffer.wrap(bArrS3);
                    byteBufferWrap2.order(ByteOrder.LITTLE_ENDIAN);
                    t().F = byteBufferWrap2.getInt(0);
                } catch (Exception e3) {
                    i.D0(e3.toString());
                }
            }
        }
        if (this.K0 != null) {
            i.U3("read patch extension version");
            byte[] bArrS4 = S(this.K0);
            if (bArrS4 != null) {
                ByteBuffer byteBufferWrap3 = ByteBuffer.wrap(bArrS4);
                byteBufferWrap3.order(ByteOrder.LITTLE_ENDIAN);
                t().G = byteBufferWrap3.getShort(0) & 65535;
            }
        }
    }

    public boolean t0() {
        BluetoothGattCharacteristic bluetoothGattCharacteristic = this.H0;
        if (bluetoothGattCharacteristic == null) {
            return false;
        }
        byte[] bArrS = S(bluetoothGattCharacteristic);
        if (bArrS == null || bArrS.length < 6) {
            i.W3("Get remote dev Mac Addr info failed, do nothing.");
            throw new g.i.a.c.g.c("remote dev Mac Addr info error", 277);
        }
        byte[] bArr = new byte[6];
        System.arraycopy(bArrS, 0, bArr, 0, 6);
        t().D = bArr;
        return true;
    }

    public boolean u0() {
        try {
            i.x0(this.f5594f, "<< OPCODE_DFU_RESET_SYSTEM (0x05)");
            return R(this.F0, 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;
        }
    }

    public void v0() {
        i.U3("<< OPCODE_DFU_START_DFU(0x01)");
        byte[] bArr = new byte[16];
        System.arraycopy(this.G.f5567r, 0, bArr, 0, 12);
        byte[] bArr2 = new byte[17];
        bArr2[0] = 1;
        if (t().f5681q) {
            System.arraycopy(this.L.a(bArr, 0, 16), 0, bArr2, 1, 16);
        } else {
            System.arraycopy(bArr, 0, bArr2, 1, 16);
        }
        R(this.F0, bArr2, false);
        if (this.f5594f) {
            i.U3("... Reading OPCODE_DFU_START_DFU(0x01) notification");
        }
        byte b2 = c0()[2];
        if (b2 != 1) {
            throw g.a.a.a.a.y0("0x%02X(not supported), start dfu failed", new Object[]{Byte.valueOf(b2)}, "start dfu failed", 766);
        }
    }
}
