aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-09-29 19:17:55 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2017-09-29 19:17:55 +1300
commitcff01fd43198ce786981dd8a51061dd0d415ccfd (patch)
tree27aa387988e9308788100f7fc4ee25caedf4ff2a
parentd793e868311918f039fc201fbe2608c6bd5ae3f3 (diff)
downloadtrackermap-server-cff01fd43198ce786981dd8a51061dd0d415ccfd.tar.gz
trackermap-server-cff01fd43198ce786981dd8a51061dd0d415ccfd.tar.bz2
trackermap-server-cff01fd43198ce786981dd8a51061dd0d415ccfd.zip
Improve GT06 frame decoder
-rw-r--r--src/org/traccar/protocol/Gt06FrameDecoder.java19
-rw-r--r--test/org/traccar/protocol/Gt06FrameDecoderTest.java12
2 files changed, 21 insertions, 10 deletions
diff --git a/src/org/traccar/protocol/Gt06FrameDecoder.java b/src/org/traccar/protocol/Gt06FrameDecoder.java
index f35af6572..9b93979e4 100644
--- a/src/org/traccar/protocol/Gt06FrameDecoder.java
+++ b/src/org/traccar/protocol/Gt06FrameDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2014 - 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2014 - 2017 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ public class Gt06FrameDecoder extends FrameDecoder {
protected Object decode(
ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
- // Check minimum length
if (buf.readableBytes() < 5) {
return null;
}
@@ -35,19 +34,19 @@ public class Gt06FrameDecoder extends FrameDecoder {
if (buf.getByte(buf.readerIndex()) == 0x78) {
length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2);
-
- int type = buf.getUnsignedByte(buf.readerIndex() + 3);
- if (type == Gt06ProtocolDecoder.MSG_STATUS && length == 13) {
- length += 2; // workaround for #1727
- }
-
} else {
length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2);
}
- // Check length and return buffer
if (buf.readableBytes() >= length) {
- return buf.readBytes(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());
+ }
+ }
}
return null;
diff --git a/test/org/traccar/protocol/Gt06FrameDecoderTest.java b/test/org/traccar/protocol/Gt06FrameDecoderTest.java
index 12ee6bb60..78d1eb920 100644
--- a/test/org/traccar/protocol/Gt06FrameDecoderTest.java
+++ b/test/org/traccar/protocol/Gt06FrameDecoderTest.java
@@ -12,6 +12,18 @@ public class Gt06FrameDecoderTest extends ProtocolTest {
Gt06FrameDecoder decoder = new Gt06FrameDecoder();
Assert.assertEquals(
+ binary("787800691709261259400700cc0400d376714600d37a3d5000d37a3c5000d393505a00d3765d5a00d376735a00d32e6b640d0a"),
+ decoder.decode(null, null, binary("787800691709261259400700cc0400d376714600d37a3d5000d37a3c5000d393505a00d3765d5a00d376735a00d32e6b640d0a")));
+
+ Assert.assertEquals(
+ binary("7878121011091c0b1e2e98058507f80097a6ac03344a0d0a"),
+ decoder.decode(null, null, binary("7878121011091c0b1e2e98058507f80097a6ac03344a0d0a")));
+
+ Assert.assertEquals(
+ binary("787808171709281135331491827b75594dc8d719a9708452cad719a9708550cad719a97086521491827b75574cac9e17b308085dc8d71939633947cad71939633a480700cc0400d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a0d0a"),
+ decoder.decode(null, null, binary("787808171709281135331491827b75594dc8d719a9708452cad719a9708550cad719a97086521491827b75574cac9e17b308085dc8d71939633947cad71939633a480700cc0400d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a00d37a3d5a0d0a")));
+
+ Assert.assertEquals(
binary("78781f1210020e140613cc04770690003e3f2e3414b20000000000000000044c446a0d0a"),
decoder.decode(null, null, binary("78781f1210020e140613cc04770690003e3f2e3414b20000000000000000044c446a0d0a")));