Search in sources :

Example 66 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class BFH16DeviceSupport method onReset.

// TODO: check
@Override
public void onReset(int flags) {
    try {
        TransactionBuilder builder = performInitialized("Reboot");
        builder.write(ctrlCharacteristic, commandWithChecksum(BFH16Constants.CMD_ACTION_REBOOT_DEVICE, 0, 0));
        builder.queue(getQueue());
    } catch (Exception e) {
        LOG.warn(e.getMessage());
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 67 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class InitOperation2021 method onCharacteristicChanged.

@Override
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    UUID characteristicUUID = characteristic.getUuid();
    if (HuamiService.UUID_CHARACTERISTIC_CHUNKEDTRANSFER_2021_READ.equals(characteristicUUID)) {
        byte[] value = characteristic.getValue();
        if (value.length > 1 && value[0] == 0x03) {
            int sequenceNumber = value[4];
            int headerSize;
            if (sequenceNumber == 0 && value[9] == (byte) HuamiService.CHUNKED2021_ENDPOINT_AUTH && value[10] == 0x00 && value[11] == 0x10 && value[12] == 0x04 && value[13] == 0x01) {
                reassembleBuffer_pointer = 0;
                headerSize = 14;
                reassembleBuffer_expectedBytes = value[5] - 3;
            } else if (sequenceNumber > 0) {
                if (sequenceNumber != lastSequenceNumber + 1) {
                    LOG.warn("unexpected sequence number");
                    return false;
                }
                headerSize = 5;
            } else if (value[9] == (byte) HuamiService.CHUNKED2021_ENDPOINT_AUTH && value[10] == 0x00 && value[11] == 0x10 && value[12] == 0x05 && value[13] == 0x01) {
                try {
                    TransactionBuilder builder = createTransactionBuilder("Authenticated, now initialize phase 2");
                    builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
                    huamiSupport.enableFurtherNotifications(builder, true);
                    huamiSupport.requestDeviceInfo(builder);
                    huamiSupport.phase2Initialize(builder);
                    huamiSupport.phase3Initialize(builder);
                    huamiSupport.setInitialized(builder);
                    huamiSupport.performImmediately(builder);
                } catch (Exception e) {
                    LOG.error("failed initializing device", e);
                }
                return true;
            } else {
                LOG.info("Unhandled characteristic changed: " + characteristicUUID);
                return super.onCharacteristicChanged(gatt, characteristic);
            }
            int bytesToCopy = value.length - headerSize;
            System.arraycopy(value, headerSize, reassembleBuffer, reassembleBuffer_pointer, bytesToCopy);
            reassembleBuffer_pointer += bytesToCopy;
            lastSequenceNumber = sequenceNumber;
            if (reassembleBuffer_pointer == reassembleBuffer_expectedBytes) {
                System.arraycopy(reassembleBuffer, 0, remoteRandom, 0, 16);
                System.arraycopy(reassembleBuffer, 16, remotePublicEC, 0, 48);
                sharedEC = ecdh_generate_shared(privateEC, remotePublicEC);
                huamiSupport.encryptedSequenceNr = ((sharedEC[0] & 0xff) | ((sharedEC[1] & 0xff) << 8) | ((sharedEC[2] & 0xff) << 16) | ((sharedEC[3] & 0xff) << 24));
                byte[] secretKey = getSecretKey();
                for (int i = 0; i < 16; i++) {
                    finalSharedSessionAES[i] = (byte) (sharedEC[i + 8] ^ secretKey[i]);
                }
                huamiSupport.sharedSessionKey = finalSharedSessionAES;
                try {
                    byte[] encryptedRandom1 = CryptoUtils.encryptAES(remoteRandom, secretKey);
                    byte[] encryptedRandom2 = CryptoUtils.encryptAES(remoteRandom, finalSharedSessionAES);
                    if (encryptedRandom1.length == 16 && encryptedRandom2.length == 16) {
                        byte[] command = new byte[33];
                        command[0] = 0x05;
                        System.arraycopy(encryptedRandom1, 0, command, 1, 16);
                        System.arraycopy(encryptedRandom2, 0, command, 17, 16);
                        TransactionBuilder builder = createTransactionBuilder("Sending double encryted random to device");
                        huamiSupport.writeToChunked2021(builder, HuamiService.CHUNKED2021_ENDPOINT_AUTH, huamiSupport.getNextHandle(), command, true, false);
                        huamiSupport.performImmediately(builder);
                    }
                } catch (Exception e) {
                    LOG.error("AES encryption failed", e);
                }
            }
            return true;
        }
        huamiSupport.logMessageContent(value);
        return super.onCharacteristicChanged(gatt, characteristic);
    } else {
        LOG.info("Unhandled characteristic changed: " + characteristicUUID);
        return super.onCharacteristicChanged(gatt, characteristic);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) UUID(java.util.UUID) SetDeviceStateAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)

Example 68 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class UpdateFirmwareOperation2020 method sendTransferStart.

protected void sendTransferStart() throws IOException {
    TransactionBuilder builder = performInitialized("trasfer complete");
    builder.write(fwCControlChar, new byte[] { COMMAND_START_TRANSFER, 1 });
    builder.queue(getQueue());
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)

Example 69 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class UpdateFirmwareOperation2020 method requestParameters.

public boolean requestParameters() {
    try {
        TransactionBuilder builder = performInitialized("get update capabilities");
        byte[] bytes = new byte[] { COMMAND_REQUEST_PARAMETERS };
        builder.write(fwCControlChar, bytes);
        builder.queue(getQueue());
        return true;
    } catch (IOException e) {
        LOG.error("Error sending firmware info: " + e.getLocalizedMessage(), e);
        return false;
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 70 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class UpdateFirmwareOperation2020 method sendFinalize.

protected void sendFinalize() throws IOException {
    TransactionBuilder builder = performInitialized("finalize firmware");
    builder.write(fwCControlChar, new byte[] { COMMAND_FINALIZE_UPDATE });
    builder.queue(getQueue());
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)

Aggregations

TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)279 IOException (java.io.IOException)218 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)17 Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 GregorianCalendar (java.util.GregorianCalendar)11 UUID (java.util.UUID)11 Calendar (java.util.Calendar)10 SetDeviceBusyAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction)10 ByteBuffer (java.nio.ByteBuffer)8 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)8 ServerTransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.ServerTransactionBuilder)7 SetDeviceStateAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)7 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 SetProgressAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)4 AlertNotificationProfile (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile)4 NewAlert (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert)4 GBPrefs (nodomain.freeyourgadget.gadgetbridge.util.GBPrefs)4 InvalidKeyException (java.security.InvalidKeyException)3