1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-01 19:06:06 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/fit/baseTypes/BaseTypeInt.java
Daniele Gobbetti a5bf32b9f1 Garmin protocol: change naming and logic of several FIT classes
- refactor the logic of Global and Local messages
- add some Global messages with naming taken from [1]
- Global messages are not enum because there are too many
- introduce the concept of FieldDefinitionPrimitive
- add new Field Definitions
- add support for developer fields and array fields
- add test case for FIT files taken from [0]

[0] https://github.com/polyvertex/fitdecode/
[1] https://www.fitfileviewer.com/
2024-05-03 20:28:11 +02:00

58 lines
1.5 KiB
Java

package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.baseTypes;
import java.nio.ByteBuffer;
public class BaseTypeInt implements BaseTypeInterface {
private final long min;
private final long max;
private final long invalid;
private final boolean unsigned;
private final int size = 4;
BaseTypeInt(boolean unsigned, long invalid) {
if (unsigned) {
this.min = 0;
this.max = 0xffffffffL;
} else {
this.min = Integer.MIN_VALUE;
this.max = Integer.MAX_VALUE;
}
this.invalid = invalid;
this.unsigned = unsigned;
}
public int getByteSize() {
return size;
}
@Override
public Object decode(final ByteBuffer byteBuffer, int scale, int offset) {
long i = unsigned ? Integer.toUnsignedLong(byteBuffer.getInt()) : byteBuffer.getInt();
if (i < min || i > max)
return null;
if (i == invalid)
return null;
return ((i + offset) / scale);
}
@Override
public void encode(ByteBuffer byteBuffer, Object o, int scale, int offset) {
if (null == o) {
invalidate(byteBuffer);
return;
}
long l = ((Number) o).longValue() * scale - offset;
if (l < min || l > max) {
invalidate(byteBuffer);
return;
}
byteBuffer.putInt((int) l);
}
@Override
public void invalidate(ByteBuffer byteBuffer) {
byteBuffer.putInt((int) invalid);
}
}