Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TlvEncoder Date encoding for dates in a distant future (exceeding MAX_INT) #1630

Conversation

jakubsobolewskisag
Copy link
Contributor

If encoded date is in distant future (number of seconds since 1970 exceeds MAX_INT), TlvEncoder breaks because it always tries to encode dates as 4-byte integers. This fix checks whether number of seconds exceeds MAX_INT, and if it does the value is encoded in 8-bytes. Otherwise, previous behavior will be used (4-bytes).

@sbernard31
Copy link
Contributor

Thx for the contribution 🙏

Looking at LWM2M-v1.1.1@core§Table: C.-2 Data Type Mapping
about Time for TLV :

Same representation as Integer.

Does it means that Date encoding should look more like :

public static byte[] encodeInteger(Number number) {
ByteBuffer iBuf;
long lValue = number.longValue();
if (lValue >= Byte.MIN_VALUE && lValue <= Byte.MAX_VALUE) {
iBuf = ByteBuffer.allocate(1);
iBuf.put((byte) lValue);
} else if (lValue >= Short.MIN_VALUE && lValue <= Short.MAX_VALUE) {
iBuf = ByteBuffer.allocate(2);
iBuf.putShort((short) lValue);
} else if (lValue >= Integer.MIN_VALUE && lValue <= Integer.MAX_VALUE) {
iBuf = ByteBuffer.allocate(4);
iBuf.putInt((int) lValue);
} else {
iBuf = ByteBuffer.allocate(8);
iBuf.putLong(lValue);
}
return iBuf.array();
}
?

@sbernard31 sbernard31 mentioned this pull request Jul 10, 2024
3 tasks
@jakubsobolewskisag
Copy link
Contributor Author

jakubsobolewskisag commented Jul 11, 2024

Thx for the contribution 🙏

Looking at LWM2M-v1.1.1@core§Table: C.-2 Data Type Mapping about Time for TLV :

Same representation as Integer.

Does it means that Date encoding should look more like :

public static byte[] encodeInteger(Number number) {
ByteBuffer iBuf;
long lValue = number.longValue();
if (lValue >= Byte.MIN_VALUE && lValue <= Byte.MAX_VALUE) {
iBuf = ByteBuffer.allocate(1);
iBuf.put((byte) lValue);
} else if (lValue >= Short.MIN_VALUE && lValue <= Short.MAX_VALUE) {
iBuf = ByteBuffer.allocate(2);
iBuf.putShort((short) lValue);
} else if (lValue >= Integer.MIN_VALUE && lValue <= Integer.MAX_VALUE) {
iBuf = ByteBuffer.allocate(4);
iBuf.putInt((int) lValue);
} else {
iBuf = ByteBuffer.allocate(8);
iBuf.putLong(lValue);
}
return iBuf.array();
}

?

@sbernard31 Hmmm... I guess you're right. We can encode integers in 1, 2, 4 or 8 bytes. That would be the most optimal. I will change the code and add unit tests.

…ending on value.

Added unit tests to cover all encoding cases.
@jakubsobolewskisag
Copy link
Contributor Author

@sbernard31 done - I think I covered all of the edge cases in the unit tests.

@sbernard31
Copy link
Contributor

This is now integrated in master (squashed in 1 commit 2a1a8af), it should be soon available leshan sandbox and will be part of 2.0.0-M16 (#1629)

Thx @jakubsobolewskisag 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants