package g.i.a.c.b0;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.os.Handler;
import android.os.Looper;
import com.cvte.vman.instance.InvokeCmd;
import com.realsil.sdk.core.usb.GlobalUsbGatt;
import com.realsil.sdk.core.usb.UsbGatt;
import com.realsil.sdk.core.usb.UsbGattCharacteristic;
import g.h.a.i;
import g.i.a.c.p.f;
import io.netty.handler.codec.compression.Bzip2Constants;
import io.netty.handler.traffic.AbstractTrafficShapingHandler;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public abstract class c extends g.i.a.c.b0.a implements f {
    public GlobalUsbGatt i0;
    public UsbGatt j0;
    public volatile boolean k0;
    public Handler l0;
    public Runnable m0;

    public class a implements Runnable {
        public a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            c cVar = c.this;
            if (cVar.f5605w == 513) {
                cVar.O();
            }
        }
    }

    public c(Context context, g.i.a.c.q.f fVar, g.i.a.c.m.a.b bVar) {
        super(context, fVar, bVar);
        this.k0 = false;
        this.l0 = new Handler(Looper.getMainLooper());
        this.m0 = new a();
    }

    public void H(UsbGatt usbGatt) {
        if (usbGatt != null) {
            UsbDevice device = usbGatt.getDevice();
            boolean z2 = this.f5594f;
            StringBuilder sbF = g.a.a.a.a.F("close gatt connection: ");
            sbF.append(device.getDeviceName());
            i.x0(z2, sbF.toString());
            GlobalUsbGatt globalUsbGatt = this.i0;
            if (globalUsbGatt != null) {
                globalUsbGatt.closeGatt(device.getDeviceName());
            } else {
                usbGatt.close();
            }
        }
        B(1280);
    }

    @TargetApi(23)
    public boolean I(UsbGatt usbGatt, int i2) {
        this.M = 0;
        g.a.a.a.a.f0("requestMtu: ", i2, this.f5594f);
        if (!usbGatt.requestMtu(i2)) {
            i.W3("requestMtu failed");
            return false;
        }
        try {
            synchronized (this.V) {
                if (this.M == 0) {
                    if (this.f5594f) {
                        i.U3("wait mtu request callback for 15000ms");
                    }
                    this.V.wait(AbstractTrafficShapingHandler.DEFAULT_MAX_TIME);
                }
            }
        } catch (InterruptedException e2) {
            i.W3("requestMtu: Sleeping interrupted, e = " + e2);
        }
        if (this.M != 0) {
            return true;
        }
        i.x0(this.f5594f, "requestMtu No CallBack");
        return false;
    }

    public final boolean J(UsbGatt usbGatt, UsbGattCharacteristic usbGattCharacteristic, byte[] bArr, int i2) {
        String str;
        if (usbGatt == null) {
            str = "gatt == null";
        } else {
            if (usbGattCharacteristic != null) {
                if (bArr.length > i2) {
                    byte[] bArr2 = new byte[i2];
                    System.arraycopy(bArr, 0, bArr2, 0, i2);
                    bArr = bArr2;
                }
                if (this.f5594f) {
                    i.U3(String.format(Locale.US, "[TX] WRITE_TYPE_0x%02X, %s >> (%d)%s", Integer.valueOf(usbGattCharacteristic.getWriteType()), usbGattCharacteristic.getUuid(), Integer.valueOf(bArr.length), i.y(bArr)));
                }
                usbGattCharacteristic.setValue(bArr);
                return usbGatt.writeCharacteristic(usbGattCharacteristic);
            }
            str = "characteristic == null";
        }
        i.W3(str);
        return false;
    }

    public boolean K(UsbGatt usbGatt, UsbGattCharacteristic usbGattCharacteristic, byte[] bArr, int i2, boolean z2) {
        if (!z2 && this.f5600r) {
            throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
        }
        if (bArr == null || i2 < 0) {
            i.W3("value == null || size < 0");
            return false;
        }
        this.C = true;
        boolean zJ = false;
        int i3 = 0;
        while (this.C) {
            this.B = false;
            if (i3 > 0) {
                try {
                    i.x0(this.f5594f, "re-send command just wait a while");
                    Thread.sleep(1000L);
                } catch (InterruptedException e2) {
                    e2.printStackTrace();
                }
                if (!z2 && this.f5600r) {
                    throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
                }
            }
            zJ = J(usbGatt, usbGattCharacteristic, bArr, i2);
            if (zJ) {
                synchronized (this.A) {
                    try {
                        if (!this.B && this.f5605w == 515) {
                            this.A.wait(AbstractTrafficShapingHandler.DEFAULT_MAX_TIME);
                        }
                    } catch (InterruptedException e3) {
                        i.W3("mWriteLock Sleeping interrupted,e:" + e3);
                        if (this.M == 0) {
                            this.M = 259;
                        }
                    }
                }
                if (this.M == 0 && !this.B) {
                    i.W3("send command but no callback");
                    this.M = 261;
                }
            } else {
                i.W3("writePacket failed");
                this.M = 267;
                zJ = false;
            }
            if (this.M != 0 || i3 <= 3) {
                i3++;
            } else {
                i.W3("send command reach max try time");
                this.M = 268;
            }
            if (this.M != 0) {
                throw new g.i.a.c.g.c("Error while send command", this.M);
            }
        }
        return zJ;
    }

    public boolean L(UsbGattCharacteristic usbGattCharacteristic, byte[] bArr, boolean z2) {
        return K(this.j0, null, bArr, bArr != null ? bArr.length : -1, z2);
    }

    public void M(UsbGatt usbGatt) {
        int i2 = this.f5605w;
        if (i2 == 0 || i2 == 1280) {
            i.x0(this.f5594f, "already disconnect");
            return;
        }
        if (usbGatt == null) {
            i.x0(this.f5594f, "gatt == null");
            B(0);
        } else {
            B(1024);
            usbGatt.disconnect();
            D();
        }
    }

    public void N(int i2) {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e2) {
            e2.printStackTrace();
        }
        i.w0(String.format("terminateConnection, error = 0x%04X", Integer.valueOf(i2)));
        UsbGatt usbGatt = this.j0;
        if (usbGatt != null) {
            M(usbGatt);
            r().f(2);
            H(this.j0);
        }
    }

    public boolean O() {
        if (this.j0 == null) {
            i.W3("mUsbGatt == null");
            this.M = Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
            w();
            return false;
        }
        if (this.f5600r) {
            i.W3("task already aborted, ignore");
            return false;
        }
        i.x0(this.f5594f, "Attempting to start service discovery...");
        boolean zDiscoverServices = this.j0.discoverServices();
        boolean z2 = this.f5594f;
        StringBuilder sbF = g.a.a.a.a.F("discoverServices ");
        sbF.append(zDiscoverServices ? "succeed" : "failed");
        i.x0(z2, sbF.toString());
        if (!zDiscoverServices) {
            this.M = Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
            w();
        }
        return zDiscoverServices;
    }

    @Override // g.i.a.c.m.a.a
    public boolean e() {
        Handler handler = this.l0;
        if (handler != null) {
            handler.removeCallbacks(this.m0);
        }
        super.e();
        return true;
    }

    @Override // g.i.a.c.b0.a, g.i.a.c.m.a.a
    public void u() {
        super.u();
        this.i0 = GlobalUsbGatt.getInstance();
    }
}
