Move BCD functions to its own Util class

This commit is contained in:
Andreas Böhler
2020-11-20 18:33:53 +01:00
committed by Gitea
parent 4fd904d33f
commit 2ef9128cf8
3 changed files with 49 additions and 54 deletions

View File

@@ -86,6 +86,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA
import nodomain.freeyourgadget.gadgetbridge.service.devices.lenovo.operations.InitOperation; import nodomain.freeyourgadget.gadgetbridge.service.devices.lenovo.operations.InitOperation;
import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils;
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
import nodomain.freeyourgadget.gadgetbridge.util.BcdUtil;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
@@ -455,14 +456,14 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport {
private void handleTime(byte[] time) { private void handleTime(byte[] time) {
GregorianCalendar now = BLETypeConversions.createCalendar(); GregorianCalendar now = BLETypeConversions.createCalendar();
GregorianCalendar nowDevice = BLETypeConversions.createCalendar(); GregorianCalendar nowDevice = BLETypeConversions.createCalendar();
int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + Conversion.fromBcd8(time[8]); int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + BcdUtil.fromBcd8(time[8]);
nowDevice.set(year, nowDevice.set(year,
Conversion.fromBcd8(time[9]) - 1, BcdUtil.fromBcd8(time[9]) - 1,
Conversion.fromBcd8(time[10]), BcdUtil.fromBcd8(time[10]),
Conversion.fromBcd8(time[11]), BcdUtil.fromBcd8(time[11]),
Conversion.fromBcd8(time[12]), BcdUtil.fromBcd8(time[12]),
Conversion.fromBcd8(time[13])); BcdUtil.fromBcd8(time[13]));
nowDevice.set(Calendar.DAY_OF_WEEK, Conversion.fromBcd8(time[16]) + 1); nowDevice.set(Calendar.DAY_OF_WEEK, BcdUtil.fromBcd8(time[16]) + 1);
long timeDiff = (Math.abs(now.getTimeInMillis() - nowDevice.getTimeInMillis())) / 1000; long timeDiff = (Math.abs(now.getTimeInMillis() - nowDevice.getTimeInMillis())) / 1000;
@@ -494,12 +495,12 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("setTime"); TransactionBuilder builder = performInitialized("setTime");
int timezoneOffsetMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); int timezoneOffsetMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000);
int timezoneOffsetIndustrialMinutes = Math.round((Math.abs(timezoneOffsetMinutes) % 60) * 100f / 60f); int timezoneOffsetIndustrialMinutes = Math.round((Math.abs(timezoneOffsetMinutes) % 60) * 100f / 60f);
byte[] time = new byte[]{Conversion.toBcd8(calendar.get(Calendar.YEAR) % 100), byte[] time = new byte[]{BcdUtil.toBcd8(calendar.get(Calendar.YEAR) % 100),
Conversion.toBcd8(calendar.get(Calendar.MONTH) + 1), BcdUtil.toBcd8(calendar.get(Calendar.MONTH) + 1),
Conversion.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)), BcdUtil.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)),
Conversion.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)), BcdUtil.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)),
Conversion.toBcd8(calendar.get(Calendar.MINUTE)), BcdUtil.toBcd8(calendar.get(Calendar.MINUTE)),
Conversion.toBcd8(calendar.get(Calendar.SECOND)), BcdUtil.toBcd8(calendar.get(Calendar.SECOND)),
(byte) (timezoneOffsetMinutes / 60), (byte) (timezoneOffsetMinutes / 60),
(byte) timezoneOffsetIndustrialMinutes, (byte) timezoneOffsetIndustrialMinutes,
(byte) (calendar.get(Calendar.DAY_OF_WEEK) - 1) (byte) (calendar.get(Calendar.DAY_OF_WEEK) - 1)
@@ -595,8 +596,8 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport {
repetitionMask |= (alarm.getRepetition(Alarm.ALARM_SUN) ? 0x01 : 0x00); repetitionMask |= (alarm.getRepetition(Alarm.ALARM_SUN) ? 0x01 : 0x00);
if (0 < index && index < 4) { if (0 < index && index < 4) {
byte[] alarmValue = new byte[]{(byte) index, byte[] alarmValue = new byte[]{(byte) index,
Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)), BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)),
Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)), BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)),
repetitionMask, repetitionMask,
(byte) (alarm.getEnabled() ? 0x01 : 0x00), (byte) (alarm.getEnabled() ? 0x01 : 0x00),
0x00 // TODO: Unknown 0x00 // TODO: Unknown
@@ -2179,18 +2180,6 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport {
private static class Conversion { private static class Conversion {
static byte toBcd8(@IntRange(from = 0, to = 99) int value) {
int high = (value / 10) << 4;
int low = value % 10;
return (byte) (high | low);
}
static int fromBcd8(byte value) {
int high = ((value & 0xF0) >> 4) * 10;
int low = value & 0x0F;
return high + low;
}
static byte[] toByteArr16(int value) { static byte[] toByteArr16(int value) {
return new byte[]{(byte) (value >> 8), (byte) value}; return new byte[]{(byte) (value >> 8), (byte) value};
} }

View File

@@ -60,6 +60,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA
import nodomain.freeyourgadget.gadgetbridge.service.devices.watch9.operations.InitOperation; import nodomain.freeyourgadget.gadgetbridge.service.devices.watch9.operations.InitOperation;
import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils;
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
import nodomain.freeyourgadget.gadgetbridge.util.BcdUtil;
public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport {
@@ -238,14 +239,14 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport {
private void handleTime(byte[] time) { private void handleTime(byte[] time) {
GregorianCalendar now = BLETypeConversions.createCalendar(); GregorianCalendar now = BLETypeConversions.createCalendar();
GregorianCalendar nowDevice = BLETypeConversions.createCalendar(); GregorianCalendar nowDevice = BLETypeConversions.createCalendar();
int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + Conversion.fromBcd8(time[8]); int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + BcdUtil.fromBcd8(time[8]);
nowDevice.set(year, nowDevice.set(year,
Conversion.fromBcd8(time[9]) - 1, BcdUtil.fromBcd8(time[9]) - 1,
Conversion.fromBcd8(time[10]), BcdUtil.fromBcd8(time[10]),
Conversion.fromBcd8(time[11]), BcdUtil.fromBcd8(time[11]),
Conversion.fromBcd8(time[12]), BcdUtil.fromBcd8(time[12]),
Conversion.fromBcd8(time[13])); BcdUtil.fromBcd8(time[13]));
nowDevice.set(Calendar.DAY_OF_WEEK, Conversion.fromBcd8(time[16]) + 1); nowDevice.set(Calendar.DAY_OF_WEEK, BcdUtil.fromBcd8(time[16]) + 1);
long timeDiff = (Math.abs(now.getTimeInMillis() - nowDevice.getTimeInMillis())) / 1000; long timeDiff = (Math.abs(now.getTimeInMillis() - nowDevice.getTimeInMillis())) / 1000;
if (10 < timeDiff && timeDiff < 120) { if (10 < timeDiff && timeDiff < 120) {
@@ -260,12 +261,12 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("setTime"); TransactionBuilder builder = performInitialized("setTime");
int timezoneOffsetMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); int timezoneOffsetMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000);
int timezoneOffsetIndustrialMinutes = Math.round((Math.abs(timezoneOffsetMinutes) % 60) * 100f / 60f); int timezoneOffsetIndustrialMinutes = Math.round((Math.abs(timezoneOffsetMinutes) % 60) * 100f / 60f);
byte[] time = new byte[]{Conversion.toBcd8(calendar.get(Calendar.YEAR) % 100), byte[] time = new byte[]{BcdUtil.toBcd8(calendar.get(Calendar.YEAR) % 100),
Conversion.toBcd8(calendar.get(Calendar.MONTH) + 1), BcdUtil.toBcd8(calendar.get(Calendar.MONTH) + 1),
Conversion.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)), BcdUtil.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)),
Conversion.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)), BcdUtil.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)),
Conversion.toBcd8(calendar.get(Calendar.MINUTE)), BcdUtil.toBcd8(calendar.get(Calendar.MINUTE)),
Conversion.toBcd8(calendar.get(Calendar.SECOND)), BcdUtil.toBcd8(calendar.get(Calendar.SECOND)),
(byte) (timezoneOffsetMinutes / 60), (byte) (timezoneOffsetMinutes / 60),
(byte) timezoneOffsetIndustrialMinutes, (byte) timezoneOffsetIndustrialMinutes,
(byte) (calendar.get(Calendar.DAY_OF_WEEK) - 1) (byte) (calendar.get(Calendar.DAY_OF_WEEK) - 1)
@@ -358,8 +359,8 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport {
repetitionMask |= (alarm.getRepetition(Alarm.ALARM_SUN) ? 0x01 : 0x00); repetitionMask |= (alarm.getRepetition(Alarm.ALARM_SUN) ? 0x01 : 0x00);
if (0 < index && index < 4) { if (0 < index && index < 4) {
byte[] alarmValue = new byte[]{(byte) index, byte[] alarmValue = new byte[]{(byte) index,
Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)), BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)),
Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)), BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)),
repetitionMask, repetitionMask,
(byte) (alarm.getEnabled() ? 0x01 : 0x00), (byte) (alarm.getEnabled() ? 0x01 : 0x00),
0x00 // TODO: Unknown 0x00 // TODO: Unknown
@@ -602,18 +603,6 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport {
} }
private static class Conversion { private static class Conversion {
static byte toBcd8(@IntRange(from = 0, to = 99) int value) {
int high = (value / 10) << 4;
int low = value % 10;
return (byte) (high | low);
}
static int fromBcd8(byte value) {
int high = ((value & 0xF0) >> 4) * 10;
int low = value & 0x0F;
return high + low;
}
static byte[] toByteArr16(int value) { static byte[] toByteArr16(int value) {
return new byte[]{(byte) (value >> 8), (byte) value}; return new byte[]{(byte) (value >> 8), (byte) value};
} }

View File

@@ -0,0 +1,17 @@
package nodomain.freeyourgadget.gadgetbridge.util;
import androidx.annotation.IntRange;
public class BcdUtil {
public static byte toBcd8(@IntRange(from = 0, to = 99) int value) {
int high = (value / 10) << 4;
int low = value % 10;
return (byte) (high | low);
}
public static int fromBcd8(byte value) {
int high = ((value & 0xF0) >> 4) * 10;
int low = value & 0x0F;
return high + low;
}
}