Skip to content

Commit

Permalink
Workaround for GT06 frame decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Sep 30, 2017
1 parent 8e137d1 commit 81e6d58
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/org/traccar/protocol/Gt06FrameDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ protected Object decode(
length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2);
}

if (buf.readableBytes() >= length) {
if (buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) {
return buf.readBytes(length);
} else {
int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x0d);
if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) {
return buf.readBytes(endIndex + 2 - buf.readerIndex());
}
}
if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) {
return buf.readBytes(length);
}

int endIndex = buf.readerIndex() - 1;
do {
endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d);
if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) {
return buf.readBytes(endIndex + 2 - buf.readerIndex());
}
} while (endIndex > 0);

return null;
}

Expand Down
8 changes: 4 additions & 4 deletions test/org/traccar/protocol/Gt06FrameDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public void testDecode() throws Exception {

Gt06FrameDecoder decoder = new Gt06FrameDecoder();

Assert.assertEquals(
binary("78780d0103563140414198583c0d0a"),
decoder.decode(null, null, binary("78780d0103563140414198583c0d0a")));

Assert.assertEquals(
binary("787800691709261259400700cc0400d376714600d37a3d5000d37a3c5000d393505a00d3765d5a00d376735a00d32e6b640d0a"),
decoder.decode(null, null, binary("787800691709261259400700cc0400d376714600d37a3d5000d37a3c5000d393505a00d3765d5a00d376735a00d32e6b640d0a")));
Expand All @@ -23,10 +27,6 @@ public void testDecode() throws Exception {
binary("787808171709281135331491827b75594dc8d719a9708452cad719a9708550cad719a97086521491827b75574cac9e17b308085dc8d71939633947cad71939633a480700cc0400d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a0d0a"),
decoder.decode(null, null, binary("787808171709281135331491827b75594dc8d719a9708452cad719a9708550cad719a97086521491827b75574cac9e17b308085dc8d71939633947cad71939633a480700cc0400d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a0d0a")));

Assert.assertEquals(
binary("78781f1210020e140613cc04770690003e3f2e3414b20000000000000000044c446a0d0a"),
decoder.decode(null, null, binary("78781f1210020e140613cc04770690003e3f2e3414b20000000000000000044c446a0d0a")));

Assert.assertEquals(
binary("787808134606020002044dc5050d0a"),
decoder.decode(null, null, binary("787808134606020002044dc5050d0a")));
Expand Down

0 comments on commit 81e6d58

Please sign in to comment.