package com.dss.ups.sp51a.ble.manager;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.content.Context;
import android.util.Log;
import io.netty.handler.codec.rtsp.RtspHeaders;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import kotlin.Metadata;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.Lambda;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000h\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\f\u0018\u0000 82\u00020\u0001:\u00018B\u0007¢\u0006\u0004\b6\u00107J#\u0010\u0005\u001a\u00028\u0000\"\u0004\b\u0000\u0010\u00022\f\u0010\u0004\u001a\b\u0012\u0004\u0012\u00028\u00000\u0003H\u0002¢\u0006\u0004\b\u0005\u0010\u0006J1\u0010\u0010\u001a\u0004\u0018\u00010\u000f2\u0006\u0010\b\u001a\u00020\u00072\u0006\u0010\n\u001a\u00020\t2\u0006\u0010\f\u001a\u00020\u000b2\b\b\u0002\u0010\u000e\u001a\u00020\r¢\u0006\u0004\b\u0010\u0010\u0011J\u0019\u0010\u0012\u001a\u0004\u0018\u00010\u000f2\b\b\u0002\u0010\u000e\u001a\u00020\r¢\u0006\u0004\b\u0012\u0010\u0013J)\u0010\u0018\u001a\u0004\u0018\u00010\u00172\u0006\u0010\u0014\u001a\u00020\u000f2\u0006\u0010\u0016\u001a\u00020\u00152\b\b\u0002\u0010\u000e\u001a\u00020\r¢\u0006\u0004\b\u0018\u0010\u0019J'\u0010\u001b\u001a\u00020\u001a2\u0006\u0010\u0014\u001a\u00020\u000f2\u0006\u0010\u0016\u001a\u00020\u00152\b\b\u0002\u0010\u000e\u001a\u00020\r¢\u0006\u0004\b\u001b\u0010\u001cJ'\u0010\u001f\u001a\u00020\u001a2\u0006\u0010\u0014\u001a\u00020\u000f2\u0006\u0010\u001e\u001a\u00020\u001d2\b\b\u0002\u0010\u000e\u001a\u00020\r¢\u0006\u0004\b\u001f\u0010 J\u0017\u0010#\u001a\u00020\"2\b\u0010!\u001a\u0004\u0018\u00010\u0017¢\u0006\u0004\b#\u0010$J\u0015\u0010%\u001a\u00020\"2\u0006\u0010!\u001a\u00020\u001a¢\u0006\u0004\b%\u0010&J\u0017\u0010'\u001a\u00020\"2\b\u0010\u0014\u001a\u0004\u0018\u00010\u000f¢\u0006\u0004\b'\u0010(R\u0016\u0010*\u001a\u00020)8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b*\u0010+R\u0018\u0010\u0014\u001a\u0004\u0018\u00010\u000f8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0014\u0010,R\u001e\u0010/\u001a\n .*\u0004\u0018\u00010-0-8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b/\u00100R\u0018\u00101\u001a\u0004\u0018\u00010\u00178\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b1\u00102R\u0016\u00103\u001a\u00020\u001a8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b3\u00104R\u001e\u00105\u001a\n .*\u0004\u0018\u00010-0-8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b5\u00100¨\u00069"}, d2 = {"Lcom/dss/ups/sp51a/ble/manager/GattLock;", "", "T", "Lkotlin/Function0;", "let", "safeLock", "(Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;", "Landroid/content/Context;", "context", "Landroid/bluetooth/BluetoothDevice;", "device", "Landroid/bluetooth/BluetoothGattCallback;", "callback", "", RtspHeaders.Values.TIMEOUT, "Landroid/bluetooth/BluetoothGatt;", "waitForConnect", "(Landroid/content/Context;Landroid/bluetooth/BluetoothDevice;Landroid/bluetooth/BluetoothGattCallback;J)Landroid/bluetooth/BluetoothGatt;", "waitForConnectResult", "(J)Landroid/bluetooth/BluetoothGatt;", "gatt", "Landroid/bluetooth/BluetoothGattCharacteristic;", "characteristic", "", "waitForReadCharacteristic", "(Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;J)[B", "", "waitForWriteCharacteristic", "(Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;J)Z", "Landroid/bluetooth/BluetoothGattDescriptor;", "descriptor", "waitForWriteDescriptor", "(Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;J)Z", "result", "", "notifyReadLock", "([B)V", "notifyWriteLock", "(Z)V", "notifyConnect", "(Landroid/bluetooth/BluetoothGatt;)V", "Ljava/util/concurrent/locks/ReentrantLock;", "lock", "Ljava/util/concurrent/locks/ReentrantLock;", "Landroid/bluetooth/BluetoothGatt;", "Ljava/util/concurrent/locks/Condition;", "kotlin.jvm.PlatformType", "connectCondition", "Ljava/util/concurrent/locks/Condition;", "readResult", "[B", "writeResult", "Z", "configCondition", "<init>", "()V", "Companion", "lib_release"}, k = 1, mv = {1, 5, 1})
public final class GattLock {
    public static final String TAG = "GattLock";
    private final Condition configCondition;
    private final Condition connectCondition;
    private BluetoothGatt gatt;
    private final ReentrantLock lock;
    private byte[] readResult;
    private boolean writeResult;

    /* JADX INFO: renamed from: com.dss.ups.sp51a.ble.manager.GattLock$waitForConnect$1, reason: invalid class name */
    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\b\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0001\u001a\u0004\u0018\u00010\u0000H\n¢\u0006\u0004\b\u0001\u0010\u0002"}, d2 = {"Landroid/bluetooth/BluetoothGatt;", "<anonymous>", "()Landroid/bluetooth/BluetoothGatt;"}, k = 3, mv = {1, 5, 1})
    public static final class AnonymousClass1 extends Lambda implements Function0<BluetoothGatt> {
        public final /* synthetic */ BluetoothGattCallback $callback;
        public final /* synthetic */ Context $context;
        public final /* synthetic */ BluetoothDevice $device;
        public final /* synthetic */ long $timeout;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass1(BluetoothDevice bluetoothDevice, Context context, BluetoothGattCallback bluetoothGattCallback, long j2) {
            super(0);
            this.$device = bluetoothDevice;
            this.$context = context;
            this.$callback = bluetoothGattCallback;
            this.$timeout = j2;
        }

        /* JADX WARN: Can't rename method to resolve collision */
        @Override // kotlin.jvm.functions.Function0
        public final BluetoothGatt invoke() throws InterruptedException {
            GattLock.this.gatt = null;
            this.$device.connectGatt(this.$context, false, this.$callback);
            Log.d(GattLock.TAG, "waitForConnect");
            GattLock.this.connectCondition.await(this.$timeout, TimeUnit.MILLISECONDS);
            Log.d(GattLock.TAG, o.l("waitForConnect result ", GattLock.this.gatt));
            return GattLock.this.gatt;
        }
    }

    /* JADX INFO: renamed from: com.dss.ups.sp51a.ble.manager.GattLock$waitForConnectResult$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\b\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0001\u001a\u0004\u0018\u00010\u0000H\n¢\u0006\u0004\b\u0001\u0010\u0002"}, d2 = {"Landroid/bluetooth/BluetoothGatt;", "<anonymous>", "()Landroid/bluetooth/BluetoothGatt;"}, k = 3, mv = {1, 5, 1})
    public static final class C01911 extends Lambda implements Function0<BluetoothGatt> {
        public final /* synthetic */ long $timeout;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C01911(long j2) {
            super(0);
            this.$timeout = j2;
        }

        /* JADX WARN: Can't rename method to resolve collision */
        @Override // kotlin.jvm.functions.Function0
        public final BluetoothGatt invoke() throws InterruptedException {
            Log.d(GattLock.TAG, "waitForConnectResult");
            GattLock.this.connectCondition.await(this.$timeout, TimeUnit.MILLISECONDS);
            Log.d(GattLock.TAG, o.l("waitForConnectResult result ", GattLock.this.gatt));
            return GattLock.this.gatt;
        }
    }

    /* JADX INFO: renamed from: com.dss.ups.sp51a.ble.manager.GattLock$waitForReadCharacteristic$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\b\n\u0002\u0010\u0012\n\u0002\b\u0002\u0010\u0001\u001a\u0004\u0018\u00010\u0000H\n¢\u0006\u0004\b\u0001\u0010\u0002"}, d2 = {"", "<anonymous>", "()[B"}, k = 3, mv = {1, 5, 1})
    public static final class C01921 extends Lambda implements Function0<byte[]> {
        public final /* synthetic */ BluetoothGattCharacteristic $characteristic;
        public final /* synthetic */ BluetoothGatt $gatt;
        public final /* synthetic */ long $timeout;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C01921(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, long j2) {
            super(0);
            this.$gatt = bluetoothGatt;
            this.$characteristic = bluetoothGattCharacteristic;
            this.$timeout = j2;
        }

        @Override // kotlin.jvm.functions.Function0
        public final byte[] invoke() throws InterruptedException {
            GattLock.this.readResult = null;
            if (this.$gatt.readCharacteristic(this.$characteristic)) {
                GattLock.this.configCondition.await(this.$timeout, TimeUnit.MILLISECONDS);
            } else {
                Log.e(GattLock.TAG, "readCharacteristic failed");
            }
            byte[] bArr = GattLock.this.readResult;
            if (bArr == null) {
                return null;
            }
            return (byte[]) bArr.clone();
        }
    }

    /* JADX INFO: renamed from: com.dss.ups.sp51a.ble.manager.GattLock$waitForWriteCharacteristic$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\b\n\u0002\u0010\u000b\n\u0002\b\u0002\u0010\u0001\u001a\u00020\u0000H\n¢\u0006\u0004\b\u0001\u0010\u0002"}, d2 = {"", "<anonymous>", "()Z"}, k = 3, mv = {1, 5, 1})
    public static final class C01931 extends Lambda implements Function0<Boolean> {
        public final /* synthetic */ BluetoothGattCharacteristic $characteristic;
        public final /* synthetic */ BluetoothGatt $gatt;
        public final /* synthetic */ long $timeout;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C01931(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, long j2) {
            super(0);
            this.$gatt = bluetoothGatt;
            this.$characteristic = bluetoothGattCharacteristic;
            this.$timeout = j2;
        }

        @Override // kotlin.jvm.functions.Function0
        public /* bridge */ /* synthetic */ Boolean invoke() {
            return Boolean.valueOf(invoke2());
        }

        /* JADX INFO: renamed from: invoke, reason: avoid collision after fix types in other method */
        public final boolean invoke2() throws InterruptedException {
            GattLock.this.writeResult = false;
            if (this.$gatt.writeCharacteristic(this.$characteristic)) {
                GattLock.this.configCondition.await(this.$timeout, TimeUnit.MILLISECONDS);
            } else {
                Log.e(GattLock.TAG, "writeCharacteristic failed");
            }
            return GattLock.this.writeResult;
        }
    }

    /* JADX INFO: renamed from: com.dss.ups.sp51a.ble.manager.GattLock$waitForWriteDescriptor$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\b\n\u0002\u0010\u000b\n\u0002\b\u0002\u0010\u0001\u001a\u00020\u0000H\n¢\u0006\u0004\b\u0001\u0010\u0002"}, d2 = {"", "<anonymous>", "()Z"}, k = 3, mv = {1, 5, 1})
    public static final class C01941 extends Lambda implements Function0<Boolean> {
        public final /* synthetic */ BluetoothGattDescriptor $descriptor;
        public final /* synthetic */ BluetoothGatt $gatt;
        public final /* synthetic */ long $timeout;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C01941(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, long j2) {
            super(0);
            this.$gatt = bluetoothGatt;
            this.$descriptor = bluetoothGattDescriptor;
            this.$timeout = j2;
        }

        @Override // kotlin.jvm.functions.Function0
        public /* bridge */ /* synthetic */ Boolean invoke() {
            return Boolean.valueOf(invoke2());
        }

        /* JADX INFO: renamed from: invoke, reason: avoid collision after fix types in other method */
        public final boolean invoke2() throws InterruptedException {
            GattLock.this.writeResult = false;
            if (this.$gatt.writeDescriptor(this.$descriptor)) {
                GattLock.this.configCondition.await(this.$timeout, TimeUnit.MILLISECONDS);
            } else {
                Log.e(GattLock.TAG, "writeDescriptor failed");
            }
            return GattLock.this.writeResult;
        }
    }

    public GattLock() {
        ReentrantLock reentrantLock = new ReentrantLock();
        this.lock = reentrantLock;
        this.configCondition = reentrantLock.newCondition();
        this.connectCondition = reentrantLock.newCondition();
    }

    private final <T> T safeLock(Function0<? extends T> let) {
        try {
            this.lock.lock();
            return let.invoke();
        } finally {
            this.lock.unlock();
        }
    }

    public static /* synthetic */ BluetoothGatt waitForConnect$default(GattLock gattLock, Context context, BluetoothDevice bluetoothDevice, BluetoothGattCallback bluetoothGattCallback, long j2, int i2, Object obj) {
        if ((i2 & 8) != 0) {
            j2 = 10000;
        }
        return gattLock.waitForConnect(context, bluetoothDevice, bluetoothGattCallback, j2);
    }

    public static /* synthetic */ BluetoothGatt waitForConnectResult$default(GattLock gattLock, long j2, int i2, Object obj) {
        if ((i2 & 1) != 0) {
            j2 = 10000;
        }
        return gattLock.waitForConnectResult(j2);
    }

    public static /* synthetic */ byte[] waitForReadCharacteristic$default(GattLock gattLock, BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, long j2, int i2, Object obj) {
        if ((i2 & 4) != 0) {
            j2 = 3000;
        }
        return gattLock.waitForReadCharacteristic(bluetoothGatt, bluetoothGattCharacteristic, j2);
    }

    public static /* synthetic */ boolean waitForWriteCharacteristic$default(GattLock gattLock, BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, long j2, int i2, Object obj) {
        if ((i2 & 4) != 0) {
            j2 = 3000;
        }
        return gattLock.waitForWriteCharacteristic(bluetoothGatt, bluetoothGattCharacteristic, j2);
    }

    public static /* synthetic */ boolean waitForWriteDescriptor$default(GattLock gattLock, BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, long j2, int i2, Object obj) {
        if ((i2 & 4) != 0) {
            j2 = 3000;
        }
        return gattLock.waitForWriteDescriptor(bluetoothGatt, bluetoothGattDescriptor, j2);
    }

    public final void notifyConnect(BluetoothGatt gatt) {
        this.lock.lock();
        this.gatt = gatt;
        this.connectCondition.signalAll();
        this.lock.unlock();
    }

    public final void notifyReadLock(byte[] result) {
        this.lock.lock();
        this.readResult = result;
        this.configCondition.signalAll();
        this.lock.unlock();
    }

    public final void notifyWriteLock(boolean result) {
        this.lock.lock();
        this.writeResult = result;
        this.configCondition.signalAll();
        this.lock.unlock();
    }

    public final BluetoothGatt waitForConnect(Context context, BluetoothDevice device, BluetoothGattCallback callback, long timeout) {
        o.f(context, "context");
        o.f(device, "device");
        o.f(callback, "callback");
        return (BluetoothGatt) safeLock(new AnonymousClass1(device, context, callback, timeout));
    }

    public final BluetoothGatt waitForConnectResult(long timeout) {
        return (BluetoothGatt) safeLock(new C01911(timeout));
    }

    public final byte[] waitForReadCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, long timeout) {
        o.f(gatt, "gatt");
        o.f(characteristic, "characteristic");
        return (byte[]) safeLock(new C01921(gatt, characteristic, timeout));
    }

    public final synchronized boolean waitForWriteCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, long timeout) {
        o.f(gatt, "gatt");
        o.f(characteristic, "characteristic");
        return ((Boolean) safeLock(new C01931(gatt, characteristic, timeout))).booleanValue();
    }

    public final boolean waitForWriteDescriptor(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, long timeout) {
        o.f(gatt, "gatt");
        o.f(descriptor, "descriptor");
        return ((Boolean) safeLock(new C01941(gatt, descriptor, timeout))).booleanValue();
    }
}
