diff options
-rw-r--r-- | src/main/java/org/traccar/protocol/ItsFrameDecoder.java | 36 | ||||
-rw-r--r-- | src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java | 8 |
2 files changed, 31 insertions, 13 deletions
diff --git a/src/main/java/org/traccar/protocol/ItsFrameDecoder.java b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java index 79b341760..eb2c59e91 100644 --- a/src/main/java/org/traccar/protocol/ItsFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java @@ -19,30 +19,40 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import org.traccar.BaseFrameDecoder; +import org.traccar.helper.BufferUtil; public class ItsFrameDecoder extends BaseFrameDecoder { + private static final int MINIMUM_LENGTH = 20; + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { - int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); - - if (buf.writerIndex() > delimiterIndex + 1 && buf.getByte(delimiterIndex + 1) == '*') { - delimiterIndex += 1; - } - ByteBuf frame; - if (buf.getByte(delimiterIndex - 2) == ',') { - frame = buf.readRetainedSlice(delimiterIndex - 1 - buf.readerIndex()); - buf.skipBytes(1); // binary checksum - } else { + int delimiterIndex = BufferUtil.indexOf("\r\n", buf);; + if (delimiterIndex > MINIMUM_LENGTH) { frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); + buf.skipBytes(2); + return frame; + } else { + delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + if (delimiterIndex > MINIMUM_LENGTH) { + if (buf.writerIndex() > delimiterIndex + 1 && buf.getByte(delimiterIndex + 1) == '*') { + delimiterIndex += 1; + } + if (buf.getByte(delimiterIndex - 2) == ',') { + frame = buf.readRetainedSlice(delimiterIndex - 1 - buf.readerIndex()); + buf.skipBytes(1); // binary checksum + } else { + frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); + } + buf.skipBytes(1); // delimiter + return frame; + } } - buf.skipBytes(1); // delimiter - - return frame; + return null; } } diff --git a/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java b/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java index d7bd7d483..6112f773b 100644 --- a/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java +++ b/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java @@ -11,6 +11,14 @@ public class ItsFrameDecoderTest extends ProtocolTest { ItsFrameDecoder decoder = new ItsFrameDecoder(); verifyFrame( + binary("244865616465722c69547269616e676c65312c4b41303147313233342c3836343439353033343433343631302c315f333654303242303136344d4149535f362c4149533134302c302e3030303030302c4e2c302e3030303030302c452a3545"), + decoder.decode(null, null, binary("244865616465722c69547269616e676c65312c4b41303147313233342c3836343439353033343433343631302c315f333654303242303136344d4149535f362c4149533134302c302e3030303030302c4e2c302e3030303030302c452a35450d0a"))); + + verifyFrame( + binary("242c43502c41544c2c312e3444335f4149533134305f312e302c50432c31322c4c2c3836383732383033373731373434312c2c312c31363034323031392c3037333432372c32382e3633333533352c4e2c37372e3232323733332c452c302e302c3333392e30302c302c302e302c312e302c302e372c216465612c302c312c31332e372c332e392c302c4f2c32312c3430342c20342c38382c616433352c616437622c38382c32322c636661612c38382c31362c363666392c38382c31342c336632632c64372c31332c303131312c30302c3236303731312c"), + decoder.decode(null, null, binary("242c43502c41544c2c312e3444335f4149533134305f312e302c50432c31322c4c2c3836383732383033373731373434312c2c312c31363034323031392c3037333432372c32382e3633333533352c4e2c37372e3232323733332c452c302e302c3333392e30302c302c302e302c312e302c302e372c216465612c302c312c31332e372c332e392c302c4f2c32312c3430342c20342c38382c616433352c616437622c38382c32322c636661612c38382c31362c363666392c38382c31342c336632632c64372c31332c303131312c30302c3236303731312c3a2a2c4f2c532c2b393138373433393530333938"))); + + verifyFrame( binary("242c43502c41544c2c312e3444335f4149533134305f312e302c45412c31302c482c3836383732383033373731373434312c2c312c32393033323031392c3232353931332c32382e3533343031362c4e2c37372e3236383933362c452c302e302c34382e30302c302c302e302c312e322c302e372c216465612c302c312c31322e362c332e392c312c4f2c32322c3430342c20342c3161612c346230622c643433642c3161612c31352c643433622c3161612c31352c346230632c3161612c31342c346136322c3161612c31322c303131312c30302c3232313333322c"), decoder.decode(null, null, binary("242c43502c41544c2c312e3444335f4149533134305f312e302c45412c31302c482c3836383732383033373731373434312c2c312c32393033323031392c3232353931332c32382e3533343031362c4e2c37372e3236383933362c452c302e302c34382e30302c302c302e302c312e322c302e372c216465612c302c312c31322e362c332e392c312c4f2c32322c3430342c20342c3161612c346230622c643433642c3161612c31352c643433622c3161612c31352c346230632c3161612c31342c346136322c3161612c31322c303131312c30302c3232313333322cbc2a"))); |