diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/AlarmCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/AlarmCommand.java index a351920b4..b443eb6fb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/AlarmCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/AlarmCommand.java @@ -131,13 +131,13 @@ public class AlarmCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_ALARM); - if (params.limit() < 2) + if (params.limit() - params.position() < 2) throwUnexpectedLength(); op = params.get(); index = params.get(); if (op == OP_GET) { - if (params.limit() != 8) + if (params.limit() - params.position() != 8) throwUnexpectedLength(); enabled = params.get() == 1; @@ -147,7 +147,7 @@ public class AlarmCommand extends BaseCommand { hour = params.get(); minute = params.get(); } else if (op == OP_SET) { - if (params.limit() != 3) + if (params.limit() - params.position() != 3) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/BaseCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/BaseCommand.java index 486dc0b25..2a90fbcb2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/BaseCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/BaseCommand.java @@ -49,20 +49,20 @@ public abstract class BaseCommand { return makeCommand(id, buffer); } - static protected byte calculateChecksum(byte[] data, int offset, int length) { - byte checksum = 0; + public static byte calculateChecksum(byte[] data, int offset, int length) { + int checksum = 0; for (int i = offset; i < offset + length; ++i) { byte b = data[i]; for (int j = 0; j < 8; ++j) { if (((b ^ checksum) & 1) == 0) { checksum >>= 1; } else { - checksum = (byte) ((checksum ^ 0x18) >> 1 | 0x80); + checksum = (checksum ^ 0x18) >> 1 | 0x80; } b >>= 1; } } - return checksum; + return (byte)checksum; } /** @@ -98,7 +98,7 @@ public abstract class BaseCommand { protected void validateIdAndLength(byte id, ByteBuffer params, byte expectedId, int expectedLength) { validateId(id, expectedId); - if (params.limit() != expectedLength) + if (params.limit() - params.position() != expectedLength) throwUnexpectedLength(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd22Command.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd22Command.java index c40a92fc3..2cd78b5a3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd22Command.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd22Command.java @@ -54,17 +54,17 @@ public class Cmd22Command extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_UNKNOWN_22); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 3) + if (params.limit() - params.position() != 3) throwUnexpectedLength(); unknown = params.getShort(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd25Command.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd25Command.java index 9bc49813d..e90f5a410 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd25Command.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/Cmd25Command.java @@ -56,17 +56,17 @@ public class Cmd25Command extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_UNKNOWN_25); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 5) + if (params.limit() - params.position() != 5) throwUnexpectedLength(); unknown = params.getInt(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/FeaturesCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/FeaturesCommand.java index b0006d4ed..22c5fba2c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/FeaturesCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/FeaturesCommand.java @@ -65,17 +65,17 @@ public class FeaturesCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_FEATURES); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 3) + if (params.limit() - params.position() != 3) throwUnexpectedLength(); features = params.getShort(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/GetPpgDataCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/GetPpgDataCommand.java index 25a812944..d17c4b0c6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/GetPpgDataCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/GetPpgDataCommand.java @@ -66,7 +66,7 @@ public class GetPpgDataCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_PPG_DATA); - if (params.limit() < 9) + if (params.limit() - params.position() < 9) throwUnexpectedLength(); ppgType = params.get(); @@ -93,19 +93,19 @@ public class GetPpgDataCommand extends BaseCommand { throw new IllegalArgumentException("Unknown PPG type"); } - if (params.limit() < dataLength + 9) + if (params.limit() - params.position() < dataLength + 9) throwUnexpectedLength(); ppgData = new byte[dataLength]; params.get(ppgData); // Extended count/index - if (params.limit() == dataLength + 11) + if (params.limit() - params.position() == dataLength + 11) { totalRecords |= params.get() << 8; currentRecord |= params.get() << 8; } - else if (params.limit() > dataLength + 11) { + else if (params.limit() - params.position() > dataLength + 11) { throwUnexpectedLength(); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/HydrationReminderIntervalCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/HydrationReminderIntervalCommand.java index e92fa8aee..801d90fb2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/HydrationReminderIntervalCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/HydrationReminderIntervalCommand.java @@ -56,17 +56,17 @@ public class HydrationReminderIntervalCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_HYDRATION_REMINDER_INTERVAL); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); hydrationReminderInterval = params.get(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/PpgResultCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/PpgResultCommand.java index 0a55a54a8..c4d89e7f5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/PpgResultCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/PpgResultCommand.java @@ -38,7 +38,7 @@ public class PpgResultCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_PPG_RESULT); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); ppgType = params.get(); @@ -57,7 +57,7 @@ public class PpgResultCommand extends BaseCommand { throw new IllegalArgumentException("Unknown PPG type"); } - if (params.limit() != dataLength + 1) + if (params.limit() - params.position() != dataLength + 1) throwUnexpectedLength(); ppgData = new byte[dataLength]; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/ProfileCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/ProfileCommand.java index e4b806108..dc8910c89 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/ProfileCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/ProfileCommand.java @@ -94,12 +94,12 @@ public class ProfileCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_PROFILE); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 5) + if (params.limit() - params.position() != 5) throwUnexpectedLength(); gender = params.get(); @@ -107,7 +107,7 @@ public class ProfileCommand extends BaseCommand { weight = params.get(); age = params.get(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SedentaryReminderIntervalCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SedentaryReminderIntervalCommand.java index fa116134d..fcf43a255 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SedentaryReminderIntervalCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SedentaryReminderIntervalCommand.java @@ -56,17 +56,17 @@ public class SedentaryReminderIntervalCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_SEDENTARY_REMINDER_INTERVAL); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); sedentaryReminderInterval = params.get(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SettingsCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SettingsCommand.java index 1c76a8711..f0828a7d9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SettingsCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/SettingsCommand.java @@ -83,19 +83,19 @@ public class SettingsCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_SETTINGS); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 4) + if (params.limit() - params.position() != 4) throwUnexpectedLength(); option1 = params.get(); amPmIndicator = params.get(); measurementUnit = params.get(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/TimeCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/TimeCommand.java index f25a6c27f..7c35a22ad 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/TimeCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/TimeCommand.java @@ -109,12 +109,12 @@ public class TimeCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_TIME); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 7) + if (params.limit() - params.position() != 7) throwUnexpectedLength(); year = params.get(); @@ -124,7 +124,7 @@ public class TimeCommand extends BaseCommand { minute = params.get(); second = params.get(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/UiPagesCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/UiPagesCommand.java index 77deae3cc..3c2e595c5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/UiPagesCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/lefun/commands/UiPagesCommand.java @@ -61,17 +61,17 @@ public class UiPagesCommand extends BaseCommand { protected void deserializeParams(byte id, ByteBuffer params) { validateId(id, LefunConstants.CMD_UI_PAGES); - if (params.limit() < 1) + if (params.limit() - params.position() < 1) throwUnexpectedLength(); op = params.get(); if (op == OP_GET) { - if (params.limit() != 3) + if (params.limit() - params.position() != 3) throwUnexpectedLength(); pages = params.getShort(); } else if (op == OP_SET) { - if (params.limit() != 2) + if (params.limit() - params.position() != 2) throwUnexpectedLength(); setSuccess = params.get() == 1;