package g.i.a.c.v;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import g.h.a.i;
import g.i.a.c.p.a;
import g.i.a.c.p.f;
import g.i.a.c.q.j;
import io.netty.handler.codec.compression.Bzip2Constants;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import java.util.UUID;

/* JADX INFO: loaded from: classes.dex */
public final class a extends g.i.a.c.p.a {
    public BluetoothGattCharacteristic E;
    public BluetoothGattCharacteristic F;
    public BluetoothGattCharacteristic G;
    public BluetoothGattCharacteristic H;
    public BluetoothGattCharacteristic I;
    public BluetoothGattCharacteristic J;
    public BluetoothGattCharacteristic K;
    public boolean L;

    public a(int i2, boolean z2) {
        this.f5611f = i2;
        this.L = z2;
    }

    @Override // g.i.a.c.p.a
    public void b(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        bluetoothGattCharacteristic.getUuid();
        byte[] value = bluetoothGattCharacteristic.getValue();
        if (g.f5753c.equals(bluetoothGattCharacteristic.getUuid())) {
            if (value == null || value.length < 2) {
                i.W3("notification data invalid");
                return;
            }
            int i2 = value[0] & 255;
            int i3 = value[1] & 255;
            byte b2 = value[2];
            i.V3(true, String.format("responseType = %02X , requestOpCode = %02X", Integer.valueOf(i2), Integer.valueOf(i3)));
            if (i2 == 16 && i3 == 13) {
                if (b2 == 1) {
                    i().s(value, 3);
                } else {
                    i.W3("Get temp dev info failed");
                }
                n();
            }
        }
    }

    @Override // g.i.a.c.p.a
    public void c(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) {
        g.i.a.c.q.i iVarI;
        int i3;
        g.i.a.c.q.i iVarI2;
        int i4;
        g.i.a.c.q.i iVarI3;
        int i5;
        UUID uuid = bluetoothGattCharacteristic.getUuid();
        bluetoothGattCharacteristic.getValue();
        if (i2 == 0) {
            byte[] value = bluetoothGattCharacteristic.getValue();
            int length = value != null ? value.length : 0;
            if (f.a.f5639b.equals(uuid)) {
                f(value);
            } else if (f.b.f5640b.equals(uuid)) {
                StringBuilder sbF = g.a.a.a.a.F("PNP_ID: ");
                sbF.append(i.y(value));
                i.U3(sbF.toString());
                i().y(value);
            } else if (h.f5759f.equals(uuid)) {
                i().s(value, 0);
            } else if (h.f5755b.equals(uuid)) {
                if (length > 0) {
                    ByteBuffer byteBufferWrap = ByteBuffer.wrap(value);
                    byteBufferWrap.order(ByteOrder.LITTLE_ENDIAN);
                    if (length >= 6) {
                        byte[] bArr = new byte[6];
                        byteBufferWrap.get(bArr, 0, 6);
                        i().D = bArr;
                    }
                }
            } else if (h.f5756c.equals(uuid)) {
                if (length > 0) {
                    ByteBuffer byteBufferWrap2 = ByteBuffer.wrap(value);
                    byteBufferWrap2.order(ByteOrder.LITTLE_ENDIAN);
                    if (length == 2) {
                        iVarI3 = i();
                        i5 = byteBufferWrap2.getShort(0);
                    } else if (length >= 4) {
                        iVarI3 = i();
                        i5 = byteBufferWrap2.getInt(0);
                    }
                    iVarI3.E = i5 & 65535;
                }
            } else if (h.f5757d.equals(uuid)) {
                if (length > 0) {
                    ByteBuffer byteBufferWrap3 = ByteBuffer.wrap(value);
                    byteBufferWrap3.order(ByteOrder.LITTLE_ENDIAN);
                    if (length == 2) {
                        iVarI2 = i();
                        i4 = byteBufferWrap3.getShort(0) & 65535;
                    } else if (length >= 4) {
                        iVarI2 = i();
                        i4 = byteBufferWrap3.getInt(0);
                    }
                    iVarI2.F = i4;
                }
            } else if (h.f5758e.equals(uuid)) {
                ByteBuffer byteBufferWrap4 = ByteBuffer.wrap(value);
                byteBufferWrap4.order(ByteOrder.LITTLE_ENDIAN);
                if (length == 1) {
                    iVarI = i();
                    i3 = byteBufferWrap4.get(0);
                } else if (length == 2) {
                    iVarI = i();
                    i3 = byteBufferWrap4.getShort(0) & 65535;
                }
                iVarI.G = i3;
            } else {
                int iD = g.i.a.b.e.i.a.d(uuid);
                if (iD >= 65504 && iD <= 65519) {
                    i().b(value, 0);
                } else if (iD >= 65472 && iD <= 65487) {
                    i().c(iD, value);
                }
            }
        } else {
            i.E0(true, "Characteristic read error: " + i2);
            if (!h.f5759f.equals(uuid)) {
                i.w0("ignore exctption when read other info");
                return;
            }
            j(2);
        }
        n();
    }

    @Override // g.i.a.c.p.a
    public void d(g.i.a.c.a0.b bVar, String str, BluetoothGatt bluetoothGatt, BluetoothGattService bluetoothGattService, BluetoothGattService bluetoothGattService2, a.c cVar) {
        List<j> list;
        j jVar;
        String strA;
        super.d(bVar, str, bluetoothGatt, bluetoothGattService, bluetoothGattService2, cVar);
        if (this.L) {
            i.U3("No Temp OTA, no need to check ota service");
        } else {
            BluetoothGattService bluetoothGattService3 = this.f5613n;
            if (bluetoothGattService3 != null) {
                UUID uuid = h.a;
                BluetoothGattCharacteristic characteristic = bluetoothGattService3.getCharacteristic(uuid);
                this.E = characteristic;
                if (characteristic == null) {
                    i.w0("OTA_CONTROL_ENTER_OTA_MODE_CHARACTERISTIC not found");
                } else {
                    i.V3(true, "find OTA_CONTROL_ENTER_OTA_MODE_CHARACTERISTIC = " + uuid);
                }
                BluetoothGattService bluetoothGattService4 = this.f5613n;
                UUID uuid2 = h.f5755b;
                BluetoothGattCharacteristic characteristic2 = bluetoothGattService4.getCharacteristic(uuid2);
                this.F = characteristic2;
                if (characteristic2 == null) {
                    i.w0("OTA_DEVICE_MAC_CHARACTERISTIC_UUID not found");
                } else {
                    i.U3("find OTA_DEVICE_MAC_CHARACTERISTIC_UUID = " + uuid2);
                    g.a.a.a.a.V(this.F);
                }
                BluetoothGattCharacteristic characteristic3 = this.f5613n.getCharacteristic(h.f5756c);
                this.I = characteristic3;
                if (characteristic3 == null) {
                    i.w0("OTA_READ_PATCH_CHARACTERISTIC_UUID not found");
                }
                BluetoothGattCharacteristic characteristic4 = this.f5613n.getCharacteristic(h.f5757d);
                this.H = characteristic4;
                if (characteristic4 == null) {
                    i.w0("OTA_APP_VERSION_CHARACTERISTIC_UUID not found");
                }
                BluetoothGattCharacteristic characteristic5 = this.f5613n.getCharacteristic(h.f5758e);
                this.J = characteristic5;
                if (characteristic5 == null) {
                    i.w0("OTA_READ_PATCH_EXTENSION_CHARACTERISTIC_UUID not found");
                }
                BluetoothGattCharacteristic characteristic6 = this.f5613n.getCharacteristic(h.f5759f);
                this.G = characteristic6;
                if (characteristic6 == null) {
                    i.w0("OTA_DEVICE_INFO_CHARACTERISTIC_UUID not found");
                }
                int i2 = 65472;
                while (true) {
                    if (i2 > 65487) {
                        break;
                    }
                    UUID uuidA = g.i.a.b.e.i.a.a(i2);
                    BluetoothGattCharacteristic characteristic7 = this.f5613n.getCharacteristic(uuidA);
                    if (characteristic7 == null) {
                        g.a.a.a.a.v0(uuidA, g.a.a.a.a.F("not found debug characteristic:"), true);
                        break;
                    } else {
                        g.a.a.a.a.v0(uuidA, g.a.a.a.a.F("find debug characteristic: "), true);
                        this.f5617r.add(characteristic7);
                        i2++;
                    }
                }
                int i3 = 65504;
                while (true) {
                    if (i3 > 65519) {
                        break;
                    }
                    UUID uuidA2 = g.i.a.b.e.i.a.a(i3);
                    BluetoothGattCharacteristic characteristic8 = this.f5613n.getCharacteristic(uuidA2);
                    if (characteristic8 == null) {
                        g.a.a.a.a.v0(uuidA2, g.a.a.a.a.F("not found image version characteristic:"), true);
                        break;
                    } else {
                        g.a.a.a.a.v0(uuidA2, g.a.a.a.a.F("find image version characteristic: "), true);
                        this.f5617r.add(characteristic8);
                        i3++;
                    }
                }
            }
        }
        if (this.f5614o != null) {
            StringBuilder sbF = g.a.a.a.a.F("find DFU_SERVICE_UUID = ");
            sbF.append(this.f5614o.getUuid());
            i.x0(true, sbF.toString());
            if (this.L) {
                g.a.a.a.a.T(18, this.f5619t);
                BluetoothGattService bluetoothGattService5 = this.f5614o;
                UUID uuid3 = g.f5753c;
                BluetoothGattCharacteristic characteristic9 = bluetoothGattService5.getCharacteristic(uuid3);
                this.K = characteristic9;
                if (characteristic9 != null) {
                    g.a.a.a.a.i0("find DFU_CONTROL_POINT_CHARACTERISTIC: ", uuid3);
                    g.a.a.a.a.U(this.K);
                    this.K.setWriteType(2);
                    return;
                } else {
                    strA = "not found DFU_CONTROL_POINT_CHARACTERISTIC: " + uuid3;
                }
            } else {
                BluetoothGattService bluetoothGattService6 = this.f5614o;
                UUID uuid4 = g.f5754d;
                BluetoothGattCharacteristic characteristic10 = bluetoothGattService6.getCharacteristic(uuid4);
                if (characteristic10 != null) {
                    i.x0(true, "find DFU_EXTEND_FLASH_CHARACTERISTIC = " + uuid4);
                    this.f5619t.add(new j(17));
                    strA = g.i.a.b.e.g.a.a(characteristic10.getProperties());
                } else {
                    i.x0(true, "DFU_EXTEND_FLASH_CHARACTERISTIC not found");
                    g.a.a.a.a.T(16, this.f5619t);
                    if (this.E == null) {
                        return;
                    }
                    list = this.f5619t;
                    jVar = new j(0);
                }
            }
            i.w0(strA);
            return;
        }
        this.K = null;
        StringBuilder sbF2 = g.a.a.a.a.F("not find DFU_SERVICE_UUID = ");
        sbF2.append(g.a);
        i.X3(true, sbF2.toString());
        if (this.E == null) {
            return;
        }
        list = this.f5619t;
        jVar = new j(0);
        list.add(jVar);
    }

    @Override // g.i.a.c.p.a
    public void m() {
        boolean zWriteCharacteristic;
        j(257);
        if (this.f5615p != null) {
            j(Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE);
            g.a.a.a.a.l0("read battery level :", h(this.f5615p), false);
        }
        if (this.f5616q != null) {
            j(259);
            g.a.a.a.a.l0("read PnP_ID :", h(this.f5616q), false);
        }
        if (this.G != null) {
            j(260);
            boolean zH = h(this.G);
            g.a.a.a.a.l0("read device info :", zH, false);
            if (!zH) {
                this.f5617r.clear();
                j(2);
                return;
            }
        }
        if (this.F != null) {
            j(261);
            g.a.a.a.a.l0("read device mac :", h(this.F), false);
        }
        if (i().f5675k == 0) {
            if (this.H != null) {
                j(262);
                g.a.a.a.a.l0("read app version :", h(this.H), false);
            }
            if (this.I != null) {
                j(263);
                g.a.a.a.a.l0("attempt to read patch version :", h(this.I), false);
            }
            if (this.J != null) {
                j(264);
                g.a.a.a.a.l0("attempt to read patch extension version :", h(this.J), false);
            }
        }
        BluetoothGattCharacteristic bluetoothGattCharacteristic = this.K;
        if (bluetoothGattCharacteristic != null) {
            if (g(this.f5612m, bluetoothGattCharacteristic, true)) {
                j(265);
                if (this.f5612m == null || this.K == null) {
                    i.W3("mBtGatt is null maybe disconnected just now");
                    zWriteCharacteristic = false;
                } else {
                    i.U3("attempt to read temp device info ....: ");
                    this.K.setValue(new byte[]{13});
                    zWriteCharacteristic = this.f5612m.writeCharacteristic(this.K);
                    if (zWriteCharacteristic) {
                        o();
                    }
                }
                g.a.a.a.a.l0("readTempDeviceInfo:", zWriteCharacteristic, false);
            } else {
                i.x0(false, "readTempDeviceInfo failed");
            }
        }
        for (BluetoothGattCharacteristic bluetoothGattCharacteristic2 : this.f5617r) {
            int iD = g.i.a.b.e.i.a.d(bluetoothGattCharacteristic2.getUuid());
            i.V3(true, String.format("uuidShortValue=0x%4x", Integer.valueOf(iD)));
            if (iD >= 65472 && iD <= 65487) {
                j(266);
                i.x0(false, "read debug info :" + h(bluetoothGattCharacteristic2));
            } else if (iD >= 65504 && iD <= 65519 && i().f5675k != 0) {
                j(267);
                g.a.a.a.a.l0("read image version :", h(bluetoothGattCharacteristic2), false);
            }
        }
        this.f5617r.clear();
        j(1);
    }
}
