package no.nordicsemi.android.dfu;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Intent;
import android.util.Log;
import g.a.a.a.a;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.UUID;
import no.nordicsemi.android.dfu.DfuCallback;
import no.nordicsemi.android.dfu.internal.ArchiveInputStream;
import no.nordicsemi.android.dfu.internal.exception.DeviceDisconnectedException;
import no.nordicsemi.android.dfu.internal.exception.DfuException;
import no.nordicsemi.android.dfu.internal.exception.UploadAbortedException;
import no.nordicsemi.android.dfu.internal.scanner.BootloaderScannerFactory;

/* JADX INFO: loaded from: classes2.dex */
@SuppressLint({"MissingPermission"})
public abstract class BaseDfuImpl implements DfuService {
    public static final int INDICATIONS = 2;
    private static final int MAX_PACKET_SIZE_DEFAULT = 20;
    public static final int NOTIFICATIONS = 1;
    private static final String TAG = "DfuImpl";
    public boolean mAborted;
    private int mCurrentMtu;
    public int mError;
    public int mFileType;
    public InputStream mFirmwareStream;
    public BluetoothGatt mGatt;
    public int mImageSizeInBytes;
    public int mInitPacketSizeInBytes;
    public InputStream mInitPacketStream;
    public boolean mPaused;
    public DfuProgressInfo mProgressInfo;
    public boolean mRequestCompleted;
    public boolean mResetRequestSent;
    public DfuBaseService mService;
    public static final UUID GENERIC_ATTRIBUTE_SERVICE_UUID = new UUID(26392574038016L, -9223371485494954757L);
    public static final UUID SERVICE_CHANGED_UUID = new UUID(46200963207168L, -9223371485494954757L);
    public static final UUID CLIENT_CHARACTERISTIC_CONFIG = new UUID(45088566677504L, -9223371485494954757L);
    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    public final Object mLock = new Object();
    public byte[] mReceivedData = null;
    public byte[] mBuffer = new byte[20];
    public boolean mConnected = true;

    public class BaseBluetoothGattCallback extends DfuCallback.DfuGattCallback {
        public BaseBluetoothGattCallback() {
        }

        private String parse(byte[] bArr) {
            int length;
            if (bArr == null || (length = bArr.length) == 0) {
                return "";
            }
            char[] cArr = new char[(length * 3) - 1];
            for (int i2 = 0; i2 < length; i2++) {
                int i3 = bArr[i2] & 255;
                int i4 = i2 * 3;
                cArr[i4] = BaseDfuImpl.HEX_ARRAY[i3 >>> 4];
                cArr[i4 + 1] = BaseDfuImpl.HEX_ARRAY[i3 & 15];
                if (i2 != length - 1) {
                    cArr[i4 + 2] = '-';
                }
            }
            return new String(cArr);
        }

        private String phyToString(int i2) {
            return i2 != 1 ? i2 != 2 ? i2 != 3 ? a.n("UNKNOWN (", i2, ")") : "LE Coded" : "LE 2M" : "LE 1M";
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) {
            BaseDfuImpl baseDfuImpl = BaseDfuImpl.this;
            if (i2 == 0) {
                DfuBaseService dfuBaseService = baseDfuImpl.mService;
                StringBuilder sbF = a.F("Read Response received from ");
                sbF.append(bluetoothGattCharacteristic.getUuid());
                sbF.append(", value (0x): ");
                sbF.append(parse(bluetoothGattCharacteristic));
                dfuBaseService.sendLogBroadcast(5, sbF.toString());
                BaseDfuImpl.this.mReceivedData = bluetoothGattCharacteristic.getValue();
                BaseDfuImpl.this.mRequestCompleted = true;
            } else {
                baseDfuImpl.loge("Characteristic read error: " + i2);
                BaseDfuImpl.this.mError = i2 | 16384;
            }
            BaseDfuImpl.this.notifyLock();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onDescriptorRead(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i2) {
            if (i2 != 0) {
                BaseDfuImpl.this.loge("Descriptor read error: " + i2);
                BaseDfuImpl.this.mError = i2 | 16384;
            } else if (BaseDfuImpl.CLIENT_CHARACTERISTIC_CONFIG.equals(bluetoothGattDescriptor.getUuid())) {
                DfuBaseService dfuBaseService = BaseDfuImpl.this.mService;
                StringBuilder sbF = a.F("Read Response received from descr.");
                sbF.append(bluetoothGattDescriptor.getCharacteristic().getUuid());
                sbF.append(", value (0x): ");
                sbF.append(parse(bluetoothGattDescriptor));
                dfuBaseService.sendLogBroadcast(5, sbF.toString());
                if (BaseDfuImpl.SERVICE_CHANGED_UUID.equals(bluetoothGattDescriptor.getCharacteristic().getUuid())) {
                    BaseDfuImpl.this.mRequestCompleted = true;
                } else {
                    BaseDfuImpl.this.loge("Unknown descriptor read");
                }
            }
            BaseDfuImpl.this.notifyLock();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i2) {
            DfuBaseService dfuBaseService;
            StringBuilder sb;
            String str;
            if (i2 != 0) {
                BaseDfuImpl.this.loge("Descriptor write error: " + i2);
                BaseDfuImpl.this.mError = i2 | 16384;
            } else if (BaseDfuImpl.CLIENT_CHARACTERISTIC_CONFIG.equals(bluetoothGattDescriptor.getUuid())) {
                DfuBaseService dfuBaseService2 = BaseDfuImpl.this.mService;
                StringBuilder sbF = a.F("Data written to descr.");
                sbF.append(bluetoothGattDescriptor.getCharacteristic().getUuid());
                sbF.append(", value (0x): ");
                sbF.append(parse(bluetoothGattDescriptor));
                dfuBaseService2.sendLogBroadcast(5, sbF.toString());
                if (BaseDfuImpl.SERVICE_CHANGED_UUID.equals(bluetoothGattDescriptor.getCharacteristic().getUuid())) {
                    dfuBaseService = BaseDfuImpl.this.mService;
                    sb = new StringBuilder();
                    str = "Indications enabled for ";
                } else {
                    dfuBaseService = BaseDfuImpl.this.mService;
                    sb = new StringBuilder();
                    str = "Notifications enabled for ";
                }
                sb.append(str);
                sb.append(bluetoothGattDescriptor.getCharacteristic().getUuid());
                dfuBaseService.sendLogBroadcast(1, sb.toString());
            }
            BaseDfuImpl.this.notifyLock();
        }

        @Override // no.nordicsemi.android.dfu.DfuCallback.DfuGattCallback
        public void onDisconnected() {
            BaseDfuImpl baseDfuImpl = BaseDfuImpl.this;
            baseDfuImpl.mConnected = false;
            baseDfuImpl.notifyLock();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onMtuChanged(BluetoothGatt bluetoothGatt, int i2, int i3) {
            BaseDfuImpl baseDfuImpl = BaseDfuImpl.this;
            if (i3 == 0) {
                baseDfuImpl.mService.sendLogBroadcast(5, "MTU changed to: " + i2);
                int i4 = i2 + (-3);
                BaseDfuImpl baseDfuImpl2 = BaseDfuImpl.this;
                if (i4 > baseDfuImpl2.mBuffer.length) {
                    baseDfuImpl2.mBuffer = new byte[i4];
                }
                baseDfuImpl2.logi("MTU changed to: " + i2);
            } else {
                baseDfuImpl.logw(a.p("Changing MTU failed: ", i3, " (mtu: ", i2, ")"));
                if (i3 == 4 && BaseDfuImpl.this.mCurrentMtu > 23) {
                    int i5 = BaseDfuImpl.this.mCurrentMtu - 3;
                    BaseDfuImpl baseDfuImpl3 = BaseDfuImpl.this;
                    if (i5 > baseDfuImpl3.mBuffer.length) {
                        baseDfuImpl3.mBuffer = new byte[baseDfuImpl3.mCurrentMtu - 3];
                        BaseDfuImpl baseDfuImpl4 = BaseDfuImpl.this;
                        StringBuilder sbF = a.F("MTU restored to: ");
                        sbF.append(BaseDfuImpl.this.mCurrentMtu);
                        baseDfuImpl4.logi(sbF.toString());
                    }
                }
            }
            BaseDfuImpl baseDfuImpl5 = BaseDfuImpl.this;
            baseDfuImpl5.mRequestCompleted = true;
            baseDfuImpl5.notifyLock();
        }

        @Override // android.bluetooth.BluetoothGattCallback
        public void onPhyUpdate(BluetoothGatt bluetoothGatt, int i2, int i3, int i4) {
            if (i4 != 0) {
                BaseDfuImpl baseDfuImpl = BaseDfuImpl.this;
                StringBuilder sbI = a.I("Updating PHY failed: ", i4, " (txPhy: ", i2, ", rxPhy: ");
                sbI.append(i3);
                sbI.append(")");
                baseDfuImpl.logw(sbI.toString());
                return;
            }
            DfuBaseService dfuBaseService = BaseDfuImpl.this.mService;
            StringBuilder sbF = a.F("PHY updated (TX: ");
            sbF.append(phyToString(i2));
            sbF.append(", RX: ");
            sbF.append(phyToString(i3));
            sbF.append(")");
            dfuBaseService.sendLogBroadcast(5, sbF.toString());
            BaseDfuImpl baseDfuImpl2 = BaseDfuImpl.this;
            StringBuilder sbF2 = a.F("PHY updated (TX: ");
            sbF2.append(phyToString(i2));
            sbF2.append(", RX: ");
            sbF2.append(phyToString(i3));
            sbF2.append(")");
            baseDfuImpl2.logi(sbF2.toString());
        }

        public String parse(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            return parse(bluetoothGattCharacteristic.getValue());
        }

        public String parse(BluetoothGattDescriptor bluetoothGattDescriptor) {
            return parse(bluetoothGattDescriptor.getValue());
        }
    }

    public BaseDfuImpl(Intent intent, DfuBaseService dfuBaseService) {
        this.mService = dfuBaseService;
        this.mProgressInfo = dfuBaseService.mProgressInfo;
    }

    private boolean createBondApi18(BluetoothDevice bluetoothDevice) {
        try {
            Method method = bluetoothDevice.getClass().getMethod("createBond", new Class[0]);
            this.mService.sendLogBroadcast(0, "gatt.getDevice().createBond() (hidden)");
            return ((Boolean) method.invoke(bluetoothDevice, new Object[0])).booleanValue();
        } catch (Exception e2) {
            Log.w(TAG, "An exception occurred while creating bond", e2);
            return false;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:36:0x007d  */
    /* JADX WARN: Removed duplicated region for block: B:50:0x00b3  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private boolean isServiceChangedCCCDEnabled() throws no.nordicsemi.android.dfu.internal.exception.UploadAbortedException, no.nordicsemi.android.dfu.internal.exception.DfuException, no.nordicsemi.android.dfu.internal.exception.DeviceDisconnectedException {
        /*
            Method dump skipped, instruction units count: 201
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: no.nordicsemi.android.dfu.BaseDfuImpl.isServiceChangedCCCDEnabled():boolean");
    }

    @Override // no.nordicsemi.android.dfu.DfuController
    public void abort() {
        this.mPaused = false;
        this.mAborted = true;
        notifyLock();
    }

    public boolean createBond() {
        BluetoothDevice device = this.mGatt.getDevice();
        this.mRequestCompleted = false;
        this.mService.sendLogBroadcast(1, "Starting pairing...");
        this.mService.sendLogBroadcast(0, "gatt.getDevice().createBond()");
        boolean zCreateBond = device.createBond();
        try {
            synchronized (this.mLock) {
                while (zCreateBond) {
                    if (this.mRequestCompleted || this.mAborted) {
                        break;
                    }
                    this.mLock.wait();
                }
            }
        } catch (InterruptedException e2) {
            loge("Sleeping interrupted", e2);
        }
        return zCreateBond;
    }

    public void enableCCCD(BluetoothGattCharacteristic bluetoothGattCharacteristic, int i2) throws UploadAbortedException, DfuException, DeviceDisconnectedException {
        BluetoothGatt bluetoothGatt = this.mGatt;
        String str = i2 == 1 ? "notifications" : "indications";
        if (!this.mConnected) {
            throw new DeviceDisconnectedException(a.u("Unable to set ", str, " state: device disconnected"));
        }
        if (this.mAborted) {
            throw new UploadAbortedException();
        }
        this.mReceivedData = null;
        this.mError = 0;
        BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
        boolean z2 = descriptor.getValue() != null && descriptor.getValue().length == 2 && descriptor.getValue()[0] > 0 && descriptor.getValue()[1] == 0;
        if (z2) {
            return;
        }
        logi("Enabling " + str + "...");
        DfuBaseService dfuBaseService = this.mService;
        StringBuilder sbK = a.K("Enabling ", str, " for ");
        sbK.append(bluetoothGattCharacteristic.getUuid());
        dfuBaseService.sendLogBroadcast(1, sbK.toString());
        DfuBaseService dfuBaseService2 = this.mService;
        StringBuilder sbF = a.F("gatt.setCharacteristicNotification(");
        sbF.append(bluetoothGattCharacteristic.getUuid());
        sbF.append(", true)");
        dfuBaseService2.sendLogBroadcast(0, sbF.toString());
        bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true);
        descriptor.setValue(i2 == 1 ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        DfuBaseService dfuBaseService3 = this.mService;
        StringBuilder sbF2 = a.F("gatt.writeDescriptor(");
        sbF2.append(descriptor.getUuid());
        sbF2.append(i2 == 1 ? ", value=0x01-00)" : ", value=0x02-00)");
        dfuBaseService3.sendLogBroadcast(0, sbF2.toString());
        bluetoothGatt.writeDescriptor(descriptor);
        try {
            synchronized (this.mLock) {
                while (true) {
                    if (!z2) {
                        if (!this.mConnected || this.mError != 0) {
                        }
                        this.mLock.wait();
                        z2 = descriptor.getValue() == null && descriptor.getValue().length == 2 && descriptor.getValue()[0] > 0 && descriptor.getValue()[1] == 0;
                    }
                    if (!this.mPaused) {
                        break;
                    }
                    this.mLock.wait();
                    if (descriptor.getValue() == null) {
                    }
                }
            }
        } catch (InterruptedException e2) {
            loge("Sleeping interrupted", e2);
        }
        if (!this.mConnected) {
            throw new DeviceDisconnectedException(a.u("Unable to set ", str, " state: device disconnected"));
        }
        if (this.mError != 0) {
            throw new DfuException(a.u("Unable to set ", str, " state"), this.mError);
        }
    }

    @Override // no.nordicsemi.android.dfu.DfuService
    public boolean initialize(Intent intent, BluetoothGatt bluetoothGatt, int i2, InputStream inputStream, InputStream inputStream2) throws UploadAbortedException, DfuException, DeviceDisconnectedException {
        int iAvailable;
        BluetoothGattService service;
        BluetoothGattCharacteristic characteristic;
        this.mGatt = bluetoothGatt;
        this.mFileType = i2;
        this.mFirmwareStream = inputStream;
        this.mInitPacketStream = inputStream2;
        int intExtra = intent.getIntExtra(DfuBaseService.EXTRA_PART_CURRENT, 1);
        int intExtra2 = intent.getIntExtra(DfuBaseService.EXTRA_PARTS_TOTAL, 1);
        this.mCurrentMtu = intent.getIntExtra(DfuBaseService.EXTRA_CURRENT_MTU, 23);
        if (i2 > 4) {
            logw("DFU target does not support (SD/BL)+App update, splitting into 2 parts");
            this.mService.sendLogBroadcast(15, "Sending system components");
            int i3 = this.mFileType & (-5);
            this.mFileType = i3;
            ((ArchiveInputStream) this.mFirmwareStream).setContentType(i3);
            intExtra2 = 2;
        }
        if (intExtra == 2) {
            this.mService.sendLogBroadcast(15, "Sending application");
        }
        int iAvailable2 = 0;
        if (inputStream2 != null) {
            try {
                if (inputStream2.markSupported()) {
                    inputStream2.reset();
                }
                iAvailable = inputStream2.available();
            } catch (Exception unused) {
                iAvailable = 0;
            }
        } else {
            iAvailable = 0;
        }
        this.mInitPacketSizeInBytes = iAvailable;
        try {
            if (inputStream.markSupported()) {
                if (inputStream instanceof ArchiveInputStream) {
                    ((ArchiveInputStream) inputStream).fullReset();
                } else {
                    inputStream.reset();
                }
            }
            iAvailable2 = inputStream.available();
        } catch (Exception unused2) {
        }
        this.mImageSizeInBytes = iAvailable2;
        this.mProgressInfo.init(iAvailable2, intExtra, intExtra2);
        if (bluetoothGatt.getDevice().getBondState() == 12 && (service = bluetoothGatt.getService(GENERIC_ATTRIBUTE_SERVICE_UUID)) != null && (characteristic = service.getCharacteristic(SERVICE_CHANGED_UUID)) != null) {
            if (!isServiceChangedCCCDEnabled()) {
                enableCCCD(characteristic, 2);
            }
            logi("Service Changed indications enabled");
            this.mService.sendLogBroadcast(10, "Service Changed indications enabled");
        }
        return true;
    }

    public boolean isBonded() {
        return this.mGatt.getDevice().getBondState() == 12;
    }

    public void loge(String str) {
        Log.e(TAG, str);
    }

    public void loge(String str, Throwable th) {
        Log.e(TAG, str, th);
    }

    public void logi(String str) {
        if (DfuBaseService.DEBUG) {
            Log.i(TAG, str);
        }
    }

    public void logw(String str) {
        if (DfuBaseService.DEBUG) {
            Log.w(TAG, str);
        }
    }

    public void notifyLock() {
        synchronized (this.mLock) {
            this.mLock.notifyAll();
        }
    }

    @Override // no.nordicsemi.android.dfu.DfuCallback
    public void onBondStateChanged(int i2) {
        this.mRequestCompleted = true;
        notifyLock();
    }

    public String parse(byte[] bArr) {
        int length;
        if (bArr == null || (length = bArr.length) == 0) {
            return "";
        }
        char[] cArr = new char[(length * 3) - 1];
        for (int i2 = 0; i2 < length; i2++) {
            int i3 = bArr[i2] & 255;
            int i4 = i2 * 3;
            char[] cArr2 = HEX_ARRAY;
            cArr[i4] = cArr2[i3 >>> 4];
            cArr[i4 + 1] = cArr2[i3 & 15];
            if (i2 != length - 1) {
                cArr[i4 + 2] = '-';
            }
        }
        return new String(cArr);
    }

    @Override // no.nordicsemi.android.dfu.DfuController
    public void pause() {
        this.mPaused = true;
    }

    public byte[] readNotificationResponse() throws UploadAbortedException, DfuException, DeviceDisconnectedException {
        try {
            synchronized (this.mLock) {
                while (true) {
                    if ((this.mReceivedData != null || !this.mConnected || this.mError != 0 || this.mAborted) && !this.mPaused) {
                        break;
                    }
                    this.mLock.wait();
                }
            }
        } catch (InterruptedException e2) {
            loge("Sleeping interrupted", e2);
        }
        if (this.mAborted) {
            throw new UploadAbortedException();
        }
        if (!this.mConnected) {
            throw new DeviceDisconnectedException("Unable to write Op Code: device disconnected");
        }
        if (this.mError == 0) {
            return this.mReceivedData;
        }
        throw new DfuException("Unable to write Op Code", this.mError);
    }

    @Override // no.nordicsemi.android.dfu.DfuService
    public void release() {
        this.mService = null;
    }

    public boolean removeBond() {
        BluetoothDevice device = this.mGatt.getDevice();
        if (device.getBondState() == 10) {
            return true;
        }
        this.mService.sendLogBroadcast(1, "Removing bond information...");
        boolean zBooleanValue = false;
        try {
            Method method = device.getClass().getMethod("removeBond", new Class[0]);
            this.mRequestCompleted = false;
            this.mService.sendLogBroadcast(0, "gatt.getDevice().removeBond() (hidden)");
            zBooleanValue = ((Boolean) method.invoke(device, new Object[0])).booleanValue();
            try {
                synchronized (this.mLock) {
                    while (!this.mRequestCompleted && !this.mAborted) {
                        this.mLock.wait();
                    }
                }
            } catch (InterruptedException e2) {
                loge("Sleeping interrupted", e2);
            }
        } catch (Exception e3) {
            Log.w(TAG, "An exception occurred while removing bond information", e3);
        }
        return zBooleanValue;
    }

    public void requestMtu(int i2) throws UploadAbortedException, DeviceDisconnectedException {
        if (this.mAborted) {
            throw new UploadAbortedException();
        }
        this.mRequestCompleted = false;
        this.mService.sendLogBroadcast(1, "Requesting new MTU...");
        this.mService.sendLogBroadcast(0, "gatt.requestMtu(" + i2 + ")");
        if (this.mGatt.requestMtu(i2)) {
            try {
                synchronized (this.mLock) {
                    while (true) {
                        if ((this.mRequestCompleted || !this.mConnected || this.mError != 0) && !this.mPaused) {
                            break;
                        } else {
                            this.mLock.wait();
                        }
                    }
                }
            } catch (InterruptedException e2) {
                loge("Sleeping interrupted", e2);
            }
            if (!this.mConnected) {
                throw new DeviceDisconnectedException("Unable to read Service Changed CCCD: device disconnected");
            }
        }
    }

    public void restartService(Intent intent, boolean z2) {
        String strSearchUsing;
        if (z2) {
            long longExtra = intent.getLongExtra(DfuBaseService.EXTRA_SCAN_DELAY, 0L);
            long longExtra2 = intent.getLongExtra(DfuBaseService.EXTRA_SCAN_TIMEOUT, DfuServiceInitiator.DEFAULT_SCAN_TIMEOUT);
            this.mService.sendLogBroadcast(1, a.r("Scanning for the DFU Bootloader... (timeout ", longExtra2, " ms)"));
            if (longExtra > 0) {
                this.mService.waitFor(longExtra);
            }
            strSearchUsing = BootloaderScannerFactory.getScanner(this.mGatt.getDevice().getAddress()).searchUsing(this.mService.getDeviceSelector(), longExtra2);
            logi("Scanning for new address finished with: " + strSearchUsing);
            this.mService.sendLogBroadcast(5, strSearchUsing != null ? a.t("DFU Bootloader found with address ", strSearchUsing) : "DFU Bootloader not found. Trying the same address...");
        } else {
            strSearchUsing = null;
        }
        if (strSearchUsing != null) {
            intent.putExtra(DfuBaseService.EXTRA_DEVICE_ADDRESS, strSearchUsing);
        }
        intent.putExtra(DfuBaseService.EXTRA_DFU_ATTEMPT, 0);
        this.mService.startService(intent);
    }

    @Override // no.nordicsemi.android.dfu.DfuController
    public void resume() {
        this.mPaused = false;
        notifyLock();
    }

    public void waitIfPaused() {
        try {
            synchronized (this.mLock) {
                while (this.mPaused) {
                    this.mLock.wait();
                }
            }
        } catch (InterruptedException e2) {
            loge("Sleeping interrupted", e2);
        }
    }

    public void writeOpCode(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr, boolean z2) throws UploadAbortedException, DfuException, DeviceDisconnectedException {
        boolean z3;
        if (this.mAborted) {
            throw new UploadAbortedException();
        }
        this.mReceivedData = null;
        this.mError = 0;
        this.mRequestCompleted = false;
        this.mResetRequestSent = z2;
        bluetoothGattCharacteristic.setWriteType(2);
        bluetoothGattCharacteristic.setValue(bArr);
        DfuBaseService dfuBaseService = this.mService;
        StringBuilder sbF = a.F("Writing to characteristic ");
        sbF.append(bluetoothGattCharacteristic.getUuid());
        dfuBaseService.sendLogBroadcast(1, sbF.toString());
        DfuBaseService dfuBaseService2 = this.mService;
        StringBuilder sbF2 = a.F("gatt.writeCharacteristic(");
        sbF2.append(bluetoothGattCharacteristic.getUuid());
        sbF2.append(")");
        dfuBaseService2.sendLogBroadcast(0, sbF2.toString());
        this.mGatt.writeCharacteristic(bluetoothGattCharacteristic);
        try {
        } catch (InterruptedException e2) {
            loge("Sleeping interrupted", e2);
        }
        synchronized (this.mLock) {
            while (true) {
                if ((this.mRequestCompleted || !this.mConnected || this.mError != 0) && !this.mPaused) {
                    break;
                } else {
                    this.mLock.wait();
                }
                loge("Sleeping interrupted", e2);
                z3 = this.mResetRequestSent;
                if (z3 && !this.mConnected) {
                    throw new DeviceDisconnectedException(a.y(a.F("Unable to write Op Code "), bArr[0], ": device disconnected"));
                }
                if (!z3 || this.mError == 0) {
                }
                StringBuilder sbF3 = a.F("Unable to write Op Code ");
                sbF3.append((int) bArr[0]);
                throw new DfuException(sbF3.toString(), this.mError);
            }
        }
        z3 = this.mResetRequestSent;
        if (z3) {
        }
        if (z3) {
        }
    }
}
