1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-20 12:00:51 +02:00

Xiaomi: improve battery level and charger status processing

Because the reporting of battery state is inconsistent between different
models, the device's battery state was not correctly processed in GB.

For at least the firmware on the Xiaomi Watch S1 Active, the charger
state is broadcast through a separate message from the message
containing the battery level. Even though the battery level was
requested by GB upon receiving this broadcast, the charger state got
discarded as it was expected to also be included in the result of the
subsequent request.

This patch changes the name of the `Charger` message to `DeviceState`
and includes more fields that may be presented by some device models.
Furthemore, the broadcast is cached so that the charger state can be
processed from this cache instead of the battery level response message.
This commit is contained in:
MrYoranimo 2023-12-06 01:02:11 +01:00
parent 405596d960
commit b902ee96c3
2 changed files with 31 additions and 7 deletions

View File

@ -74,10 +74,11 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
public static final int CMD_PASSWORD_SET = 21;
public static final int CMD_DISPLAY_ITEMS_GET = 29;
public static final int CMD_DISPLAY_ITEMS_SET = 30;
public static final int CMD_CHARGER = 79;
public static final int CMD_DEVICE_STATE = 79;
// Not null if we're installing a firmware
private XiaomiFWHelper fwHelper = null;
private XiaomiProto.DeviceState cachedDeviceState = null;
public XiaomiSystemService(final XiaomiSupport support) {
super(support);
@ -129,8 +130,13 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
case CMD_DISPLAY_ITEMS_GET:
handleDisplayItems(cmd.getSystem().getDisplayItems());
return;
case CMD_CHARGER:
// charger event, request battery state
case CMD_DEVICE_STATE:
// some devices (e.g. Xiaomi Watch S1 Active) only broadcast the charger state through
// this message, so this will need to be kept cached to process when the battery levels
// get requested
cachedDeviceState = cmd.getSystem().getDeviceState();
// request battery state to request battery level and charger state on supported models
getSupport().sendCommand("request battery state", COMMAND_TYPE, CMD_BATTERY);
return;
}
@ -250,7 +256,16 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
final GBDeviceEventBatteryInfo batteryInfo = new GBDeviceEventBatteryInfo();
batteryInfo.batteryIndex = 0;
batteryInfo.level = battery.getLevel();
switch (battery.getState()) {
int chargerState = battery.getState();
// if device state is cached and the charging state there is set, take the charger status
// from there
if (cachedDeviceState != null && cachedDeviceState.hasChargingState()) {
chargerState = cachedDeviceState.getChargingState();
}
switch (chargerState) {
case 1:
batteryInfo.state = BatteryState.BATTERY_CHARGING;
break;

View File

@ -130,7 +130,7 @@ message System {
optional VibrationPatternAck vibrationPatternAck = 43;
// 2, 79
optional Charger charger = 49;
optional DeviceState deviceState = 49;
}
message Power {
@ -299,8 +299,17 @@ message Vibration {
optional uint32 ms = 2;
}
message Charger {
optional uint32 state = 1; // 1 charging, 2 not charging
message DeviceActivityState {
optional uint32 activityType = 1;
optional uint32 currentActivityState = 2;
}
message DeviceState {
optional uint32 chargingState = 1; // 1 charging, 2 not charging
optional uint32 wearingState = 2; // 1 wearing, 2 not wearing
optional uint32 sleepState = 3; // 1 sleep detected, 2 no sleep detected
optional uint32 warningState = 4; // ?
optional DeviceActivityState activityState = 5;
}
//