package no.nordicsemi.android.dfu;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Intent;
import java.util.Locale;
import kotlin.reflect.w.internal.y0.n.c2.c;
import no.nordicsemi.android.dfu.internal.exception.DeviceDisconnectedException;
import no.nordicsemi.android.dfu.internal.exception.DfuException;
import no.nordicsemi.android.dfu.internal.exception.RemoteDfuException;
import no.nordicsemi.android.dfu.internal.exception.UnknownResponseException;
import no.nordicsemi.android.dfu.internal.exception.UploadAbortedException;

/* JADX INFO: loaded from: classes2.dex */
public abstract class ButtonlessDfuImpl extends BaseButtonlessDfuImpl {
    private static final int DFU_STATUS_SUCCESS = 1;
    private static final byte[] OP_CODE_ENTER_BOOTLOADER = {1};
    private static final int OP_CODE_ENTER_BOOTLOADER_KEY = 1;
    private static final int OP_CODE_RESPONSE_CODE_KEY = 32;

    public ButtonlessDfuImpl(Intent intent, DfuBaseService dfuBaseService) {
        super(intent, dfuBaseService);
    }

    private int getStatusCode(byte[] bArr, int i2) throws UnknownResponseException {
        if (bArr != null && bArr.length >= 3 && bArr[0] == 32 && bArr[1] == i2 && (bArr[2] == 1 || bArr[2] == 2 || bArr[2] == 4)) {
            return bArr[2];
        }
        throw new UnknownResponseException("Invalid response received", bArr, 32, i2);
    }

    public abstract BluetoothGattCharacteristic getButtonlessDfuCharacteristic();

    public abstract int getResponseType();

    @Override // no.nordicsemi.android.dfu.DfuService
    public void performDfu(Intent intent) throws UploadAbortedException, DfuException, DeviceDisconnectedException {
        DfuBaseService dfuBaseService;
        int i2;
        byte[] notificationResponse;
        String str;
        this.mProgressInfo.setProgress(-2);
        BluetoothGatt bluetoothGatt = this.mGatt;
        this.mService.sendLogBroadcast(15, "Application with buttonless update found");
        this.mService.sendLogBroadcast(1, "Jumping to the DFU Bootloader...");
        BluetoothGattCharacteristic buttonlessDfuCharacteristic = getButtonlessDfuCharacteristic();
        int responseType = getResponseType();
        enableCCCD(buttonlessDfuCharacteristic, getResponseType());
        DfuBaseService dfuBaseService2 = this.mService;
        StringBuilder sb = new StringBuilder();
        sb.append(responseType == 2 ? "Indications" : "Notifications");
        sb.append(" enabled");
        dfuBaseService2.sendLogBroadcast(10, sb.toString());
        try {
            this.mProgressInfo.setProgress(-3);
            logi("Sending Enter Bootloader (Op Code = 1)");
            writeOpCode(buttonlessDfuCharacteristic, OP_CODE_ENTER_BOOTLOADER, true);
            this.mService.sendLogBroadcast(10, "Enter bootloader sent (Op Code = 1)");
            try {
                notificationResponse = readNotificationResponse();
            } catch (DeviceDisconnectedException unused) {
                notificationResponse = this.mReceivedData;
            }
            if (notificationResponse != null) {
                int statusCode = getStatusCode(notificationResponse, 1);
                logi("Response received (Op Code = " + ((int) notificationResponse[1]) + ", Status = " + statusCode + ")");
                this.mService.sendLogBroadcast(10, "Response received (Op Code = " + ((int) notificationResponse[1]) + ", Status = " + statusCode + ")");
                if (statusCode != 1) {
                    throw new RemoteDfuException("Device returned error after sending Enter Bootloader", statusCode);
                }
                if (shouldScanForBootloader()) {
                    this.mService.disconnect(bluetoothGatt);
                } else {
                    this.mService.waitUntilDisconnected();
                    this.mService.sendLogBroadcast(5, "Disconnected by the remote device");
                }
                str = "Device disconnected";
            } else {
                str = "Device disconnected before receiving notification";
            }
            logi(str);
            finalize(intent, false, shouldScanForBootloader());
        } catch (RemoteDfuException e2) {
            int errorNumber = e2.getErrorNumber() | 2048;
            loge(e2.getMessage());
            this.mService.sendLogBroadcast(20, String.format(Locale.US, "Remote DFU error: %s", c.p0(errorNumber)));
            dfuBaseService = this.mService;
            i2 = errorNumber | 8192;
            dfuBaseService.terminateConnection(bluetoothGatt, i2);
        } catch (UnknownResponseException e3) {
            loge(e3.getMessage());
            this.mService.sendLogBroadcast(20, e3.getMessage());
            dfuBaseService = this.mService;
            i2 = 4104;
            dfuBaseService.terminateConnection(bluetoothGatt, i2);
        }
    }

    public abstract boolean shouldScanForBootloader();
}
