1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-03 19:41:46 +02:00

Lefun: Fix response parsing

This commit is contained in:
Yukai Li 2020-10-04 20:59:55 -06:00 committed by Gitea
parent 8662041e5a
commit b46960e0a7
13 changed files with 41 additions and 41 deletions

View File

@ -131,13 +131,13 @@ public class AlarmCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_ALARM); validateId(id, LefunConstants.CMD_ALARM);
if (params.limit() < 2) if (params.limit() - params.position() < 2)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
index = params.get(); index = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 8) if (params.limit() - params.position() != 8)
throwUnexpectedLength(); throwUnexpectedLength();
enabled = params.get() == 1; enabled = params.get() == 1;
@ -147,7 +147,7 @@ public class AlarmCommand extends BaseCommand {
hour = params.get(); hour = params.get();
minute = params.get(); minute = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 3) if (params.limit() - params.position() != 3)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -49,20 +49,20 @@ public abstract class BaseCommand {
return makeCommand(id, buffer); return makeCommand(id, buffer);
} }
static protected byte calculateChecksum(byte[] data, int offset, int length) { public static byte calculateChecksum(byte[] data, int offset, int length) {
byte checksum = 0; int checksum = 0;
for (int i = offset; i < offset + length; ++i) { for (int i = offset; i < offset + length; ++i) {
byte b = data[i]; byte b = data[i];
for (int j = 0; j < 8; ++j) { for (int j = 0; j < 8; ++j) {
if (((b ^ checksum) & 1) == 0) { if (((b ^ checksum) & 1) == 0) {
checksum >>= 1; checksum >>= 1;
} else { } else {
checksum = (byte) ((checksum ^ 0x18) >> 1 | 0x80); checksum = (checksum ^ 0x18) >> 1 | 0x80;
} }
b >>= 1; 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) { protected void validateIdAndLength(byte id, ByteBuffer params, byte expectedId, int expectedLength) {
validateId(id, expectedId); validateId(id, expectedId);
if (params.limit() != expectedLength) if (params.limit() - params.position() != expectedLength)
throwUnexpectedLength(); throwUnexpectedLength();
} }

View File

@ -54,17 +54,17 @@ public class Cmd22Command extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_UNKNOWN_22); validateId(id, LefunConstants.CMD_UNKNOWN_22);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 3) if (params.limit() - params.position() != 3)
throwUnexpectedLength(); throwUnexpectedLength();
unknown = params.getShort(); unknown = params.getShort();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -56,17 +56,17 @@ public class Cmd25Command extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_UNKNOWN_25); validateId(id, LefunConstants.CMD_UNKNOWN_25);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 5) if (params.limit() - params.position() != 5)
throwUnexpectedLength(); throwUnexpectedLength();
unknown = params.getInt(); unknown = params.getInt();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -65,17 +65,17 @@ public class FeaturesCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_FEATURES); validateId(id, LefunConstants.CMD_FEATURES);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 3) if (params.limit() - params.position() != 3)
throwUnexpectedLength(); throwUnexpectedLength();
features = params.getShort(); features = params.getShort();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -66,7 +66,7 @@ public class GetPpgDataCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_PPG_DATA); validateId(id, LefunConstants.CMD_PPG_DATA);
if (params.limit() < 9) if (params.limit() - params.position() < 9)
throwUnexpectedLength(); throwUnexpectedLength();
ppgType = params.get(); ppgType = params.get();
@ -93,19 +93,19 @@ public class GetPpgDataCommand extends BaseCommand {
throw new IllegalArgumentException("Unknown PPG type"); throw new IllegalArgumentException("Unknown PPG type");
} }
if (params.limit() < dataLength + 9) if (params.limit() - params.position() < dataLength + 9)
throwUnexpectedLength(); throwUnexpectedLength();
ppgData = new byte[dataLength]; ppgData = new byte[dataLength];
params.get(ppgData); params.get(ppgData);
// Extended count/index // Extended count/index
if (params.limit() == dataLength + 11) if (params.limit() - params.position() == dataLength + 11)
{ {
totalRecords |= params.get() << 8; totalRecords |= params.get() << 8;
currentRecord |= params.get() << 8; currentRecord |= params.get() << 8;
} }
else if (params.limit() > dataLength + 11) { else if (params.limit() - params.position() > dataLength + 11) {
throwUnexpectedLength(); throwUnexpectedLength();
} }
} }

View File

@ -56,17 +56,17 @@ public class HydrationReminderIntervalCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_HYDRATION_REMINDER_INTERVAL); validateId(id, LefunConstants.CMD_HYDRATION_REMINDER_INTERVAL);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
hydrationReminderInterval = params.get(); hydrationReminderInterval = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -38,7 +38,7 @@ public class PpgResultCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_PPG_RESULT); validateId(id, LefunConstants.CMD_PPG_RESULT);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
ppgType = params.get(); ppgType = params.get();
@ -57,7 +57,7 @@ public class PpgResultCommand extends BaseCommand {
throw new IllegalArgumentException("Unknown PPG type"); throw new IllegalArgumentException("Unknown PPG type");
} }
if (params.limit() != dataLength + 1) if (params.limit() - params.position() != dataLength + 1)
throwUnexpectedLength(); throwUnexpectedLength();
ppgData = new byte[dataLength]; ppgData = new byte[dataLength];

View File

@ -94,12 +94,12 @@ public class ProfileCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_PROFILE); validateId(id, LefunConstants.CMD_PROFILE);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 5) if (params.limit() - params.position() != 5)
throwUnexpectedLength(); throwUnexpectedLength();
gender = params.get(); gender = params.get();
@ -107,7 +107,7 @@ public class ProfileCommand extends BaseCommand {
weight = params.get(); weight = params.get();
age = params.get(); age = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -56,17 +56,17 @@ public class SedentaryReminderIntervalCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_SEDENTARY_REMINDER_INTERVAL); validateId(id, LefunConstants.CMD_SEDENTARY_REMINDER_INTERVAL);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
sedentaryReminderInterval = params.get(); sedentaryReminderInterval = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -83,19 +83,19 @@ public class SettingsCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_SETTINGS); validateId(id, LefunConstants.CMD_SETTINGS);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 4) if (params.limit() - params.position() != 4)
throwUnexpectedLength(); throwUnexpectedLength();
option1 = params.get(); option1 = params.get();
amPmIndicator = params.get(); amPmIndicator = params.get();
measurementUnit = params.get(); measurementUnit = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -109,12 +109,12 @@ public class TimeCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_TIME); validateId(id, LefunConstants.CMD_TIME);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 7) if (params.limit() - params.position() != 7)
throwUnexpectedLength(); throwUnexpectedLength();
year = params.get(); year = params.get();
@ -124,7 +124,7 @@ public class TimeCommand extends BaseCommand {
minute = params.get(); minute = params.get();
second = params.get(); second = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -61,17 +61,17 @@ public class UiPagesCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_UI_PAGES); validateId(id, LefunConstants.CMD_UI_PAGES);
if (params.limit() < 1) if (params.limit() - params.position() < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() != 3) if (params.limit() - params.position() != 3)
throwUnexpectedLength(); throwUnexpectedLength();
pages = params.getShort(); pages = params.getShort();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() != 2) if (params.limit() - params.position() != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;