mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-28 19:45:50 +01:00
Lefun: Fix response parsing
This commit is contained in:
parent
8662041e5a
commit
b46960e0a7
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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];
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user