From 5f7674fe3921a68b603397e8d9bbcdff2788e9c8 Mon Sep 17 00:00:00 2001 From: vanous Date: Fri, 30 Sep 2022 00:09:42 +0200 Subject: [PATCH] SuperCars: add battery support --- .../devices/supercars/SuperCarsConstants.java | 2 ++ .../supercars/SuperCarsCoordinator.java | 2 +- .../devices/supercars/SuperCarsSupport.java | 30 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java index aabed9ba0..efff672a3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java @@ -4,6 +4,8 @@ import java.util.UUID; public class SuperCarsConstants { + public static final byte[] aes_key = new byte[]{(byte) 0x34, (byte) 0x52, (byte) 0x2A, (byte) 0x5B, (byte) 0x7A, (byte) 0x6E, (byte) 0x49, (byte) 0x2C, (byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x9D, (byte) 0x8D, (byte) 0x2A, (byte) 0x23, (byte) 0xF8}; + public static final UUID SERVICE_UUID_FFF = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"); public static final UUID CHARACTERISTIC_UUID_FFF1 = UUID.fromString("d44bc439-abfd-45a2-b575-925416129600"); public static final UUID CHARACTERISTIC_UUID_FFF2 = UUID.fromString("d44bc439-abfd-45a2-b575-92541612960a"); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsCoordinator.java index 9563931b3..42cf2f3ea 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsCoordinator.java @@ -60,7 +60,7 @@ public class SuperCarsCoordinator extends AbstractDeviceCoordinator { @Override public int getBatteryCount() { - return 0; + return 1; } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java index ffc7d8d38..8b0ed8d9a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java @@ -1,5 +1,6 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.supercars; +import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.content.BroadcastReceiver; import android.content.Context; @@ -15,9 +16,11 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.UUID; +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; import nodomain.freeyourgadget.gadgetbridge.devices.supercars.SuperCarsConstants; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.Alarm; +import nodomain.freeyourgadget.gadgetbridge.model.BatteryState; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; @@ -28,6 +31,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; +import nodomain.freeyourgadget.gadgetbridge.util.CryptoUtils; public class SuperCarsSupport extends AbstractBTLEDeviceSupport { private static final Logger LOG = LoggerFactory.getLogger(SuperCarsSupport.class); @@ -43,14 +47,19 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { @Override protected TransactionBuilder initializeDevice(TransactionBuilder builder) { builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext())); + builder.notify(getCharacteristic(SuperCarsConstants.CHARACTERISTIC_UUID_FFF4), true); //for battery + LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getContext()); IntentFilter filter = new IntentFilter(); filter.addAction(COMMAND_DRIVE_CONTROL); broadcastManager.registerReceiver(commandReceiver, filter); + getDevice().setFirmwareVersion("N/A"); + getDevice().setFirmwareVersion2("N/A"); builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext())); LOG.debug("name " + gbDevice.getName()); + return builder; } @@ -67,6 +76,27 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { } }; + @Override + public boolean onCharacteristicChanged(BluetoothGatt gatt, + BluetoothGattCharacteristic characteristic) { + super.onCharacteristicChanged(gatt, characteristic); + byte[] data = characteristic.getValue(); + byte[] decodedData = new byte[0]; + try { + decodedData = CryptoUtils.decryptAES(data, SuperCarsConstants.aes_key); + } catch (Exception e) { + LOG.error("Error while decoding received data"); + } + + if (decodedData.length == 16) { + GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); + batteryEvent.state = BatteryState.BATTERY_NORMAL; + batteryEvent.level = decodedData[4]; + evaluateGBDeviceEvent(batteryEvent); + } + return true; + } + @Override public void onNotification(NotificationSpec notificationSpec) {