diff options
author | Ivan Muratov <binakot@gmail.com> | 2017-07-14 13:29:52 +0300 |
---|---|---|
committer | Ivan Muratov <binakot@gmail.com> | 2017-07-14 13:29:52 +0300 |
commit | c8f648e0261ad46e9301db35f161140aabad880a (patch) | |
tree | 7c83f7f3d5fe02b1dc02a0cb0f3e5e93e85e59f4 | |
parent | 36eeb5c4fa86f6516585819bb76ca7b73fa4d28a (diff) | |
download | traccar-server-c8f648e0261ad46e9301db35f161140aabad880a.tar.gz traccar-server-c8f648e0261ad46e9301db35f161140aabad880a.tar.bz2 traccar-server-c8f648e0261ad46e9301db35f161140aabad880a.zip |
Upgrade the frame decoder and remove unnecessary check for the last end sign byte in protocol decoder.
Protocol decoder read the buffer until its over, because frame decoder garantee the correct package length.
Added additional tests for frame decoding.
-rw-r--r-- | src/org/traccar/protocol/Arnavi4FrameDecoder.java | 16 | ||||
-rw-r--r-- | src/org/traccar/protocol/Arnavi4ProtocolDecoder.java | 4 | ||||
-rw-r--r-- | test/org/traccar/protocol/Arnavi4FrameDecoderTest.java | 16 |
3 files changed, 28 insertions, 8 deletions
diff --git a/src/org/traccar/protocol/Arnavi4FrameDecoder.java b/src/org/traccar/protocol/Arnavi4FrameDecoder.java index a50ece641..bb130e6bb 100644 --- a/src/org/traccar/protocol/Arnavi4FrameDecoder.java +++ b/src/org/traccar/protocol/Arnavi4FrameDecoder.java @@ -50,10 +50,18 @@ public class Arnavi4FrameDecoder extends FrameDecoder { return buf.readBytes(HEADER_LENGTH); } - int parcelNumber = buf.getByte(1) & 0xFF; - if (buf.getByte(0) == PACKAGE_START_SIGN && buf.getByte(buf.readableBytes() - 1) == PACKAGE_END_SIGN - && parcelNumber >= PACKAGE_MIN_PARCEL_NUMBER && parcelNumber <= PACKAGE_MAX_PARCEL_NUMBER) { - return buf; + int index = buf.getByte(1) & 0xFF; // parcel number + if (buf.getByte(0) == PACKAGE_START_SIGN + && index >= PACKAGE_MIN_PARCEL_NUMBER && index <= PACKAGE_MAX_PARCEL_NUMBER) { + + // package: 1 byte start sign, 1 byte parcel number, packets, 1 byte end sign + int packetStartIndex = 2; + while (packetStartIndex + 8 < buf.readableBytes() && buf.getByte(packetStartIndex) != PACKAGE_END_SIGN) { + // packet: 1 byte type, 2 bytes length, 4 bytes unixtime, length-bytes data, 1 byte crc + packetStartIndex += buf.getUnsignedShort(packetStartIndex + 1) + 8; + } + + return buf.readBytes(packetStartIndex + 1); } return null; diff --git a/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java b/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java index 07e4d855c..ed11e1559 100644 --- a/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java +++ b/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java @@ -153,7 +153,7 @@ public class Arnavi4ProtocolDecoder extends BaseProtocolDecoder { int index = buf.readUnsignedByte(); byte recordType = buf.readByte(); - while (recordType != PACKAGE_END_SIGN && buf.readableBytes() != 1) { // The last end sign byte + while (buf.readableBytes() > 0) { switch (recordType) { case RECORD_PING: case RECORD_DATA: @@ -176,7 +176,7 @@ public class Arnavi4ProtocolDecoder extends BaseProtocolDecoder { return null; // Ignore unsupported types of package } - recordType = buf.readByte(); + recordType = buf.readByte(); // The last byte in package is end sign } sendPackageResponse(channel, index); diff --git a/test/org/traccar/protocol/Arnavi4FrameDecoderTest.java b/test/org/traccar/protocol/Arnavi4FrameDecoderTest.java index 0b502bc36..93d818f56 100644 --- a/test/org/traccar/protocol/Arnavi4FrameDecoderTest.java +++ b/test/org/traccar/protocol/Arnavi4FrameDecoderTest.java @@ -21,14 +21,26 @@ public class Arnavi4FrameDecoderTest extends ProtocolTest { binary(ByteOrder.LITTLE_ENDIAN, "ff23f30c45f5c90f0300"), decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "ff23f30c45f5c90f0300"))); - Assert.assertEquals( // Valid PACKAGE packet with one DATA packet + Assert.assertEquals( // Valid PACKAGE with answer to server on file transfer. + null, + decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "5bfd005d"))); + + Assert.assertEquals( // Valid PACKAGE with one DATA packet binary(ByteOrder.LITTLE_ENDIAN, "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"))); - Assert.assertEquals( // Valid PACKAGE packet with two DATA packet + Assert.assertEquals( // Valid PACKAGE with two DATA packet binary(ByteOrder.LITTLE_ENDIAN, "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"))); + Assert.assertEquals( // Valid PACKAGE with one TEXT packet. + null, + decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "030700e3f16b50747261636361721b"))); + + Assert.assertEquals( // Valid PACKAGE with one BINARY packet. + null, + decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "062000e3f16b5003298b5e4204cbd514420500191000080400ff021b"))); + } }
\ No newline at end of file |