diff options
-rw-r--r-- | src/org/traccar/protocol/TotemFrameDecoder.java | 12 | ||||
-rw-r--r-- | test/org/traccar/protocol/TotemFrameDecoderTest.java | 24 |
2 files changed, 28 insertions, 8 deletions
diff --git a/src/org/traccar/protocol/TotemFrameDecoder.java b/src/org/traccar/protocol/TotemFrameDecoder.java index 0b1a3e7c2..c6309b8d6 100644 --- a/src/org/traccar/protocol/TotemFrameDecoder.java +++ b/src/org/traccar/protocol/TotemFrameDecoder.java @@ -21,29 +21,25 @@ import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.frame.FrameDecoder; import org.traccar.helper.ChannelBufferTools; +import org.traccar.helper.StringFinder; public class TotemFrameDecoder extends FrameDecoder { @Override protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - // Check minimum length if (buf.readableBytes() < 10) { return null; } - // Find start - Integer beginIndex = ChannelBufferTools.find(buf, buf.readerIndex(), "$$"); - if (beginIndex == null) { + int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("$$")); + if (beginIndex == -1) { return null; } else if (beginIndex > buf.readerIndex()) { buf.readerIndex(beginIndex); } - // Read message int length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, Charset.defaultCharset()), 16); if (length <= buf.readableBytes()) { return buf.readBytes(length); diff --git a/test/org/traccar/protocol/TotemFrameDecoderTest.java b/test/org/traccar/protocol/TotemFrameDecoderTest.java new file mode 100644 index 000000000..4979f430a --- /dev/null +++ b/test/org/traccar/protocol/TotemFrameDecoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import org.junit.Assert; +import org.junit.Test; +import org.traccar.ProtocolDecoderTest; + +public class TotemFrameDecoderTest extends ProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + TotemFrameDecoder decoder = new TotemFrameDecoder(); + + Assert.assertEquals( + binary("242442393335363839353033373537383531387c4141244750524d432c3036313730382e3030302c412c333734302e323033332c4e2c30323132382e383132312c452c33382e38352c3237322e33362c3132313131332c2c2c412a35327c30322e337c30312e337c30312e397c3030303030303030303030307c32303133313131323036313730387c31343034313430327c30303030303030307c30303245323137317c303030307c302e323137327c383930327c34463945"), + decoder.decode(null, null, binary("242442393335363839353033373537383531387c4141244750524d432c3036313730382e3030302c412c333734302e323033332c4e2c30323132382e383132312c452c33382e38352c3237322e33362c3132313131332c2c2c412a35327c30322e337c30312e337c30312e397c3030303030303030303030307c32303133313131323036313730387c31343034313430327c30303030303030307c30303245323137317c303030307c302e323137327c383930327c344639450d0a"))); + + Assert.assertEquals( + binary("242442393335363839353033373537383531387c4141244750524d432c3036313730382e3030302c412c333734302e323033332c4e2c30323132382e383132312c452c33382e38352c3237322e33362c3132313131332c2c2c412a35327c30322e337c30312e337c30312e397c3030303030303030303030307c32303133313131323036313730387c31343034313430327c30303030303030307c30303245323137317c303030307c302e323137327c383930327c34463945"), + decoder.decode(null, null, binary("0d0a242442393335363839353033373537383531387c4141244750524d432c3036313730382e3030302c412c333734302e323033332c4e2c30323132382e383132312c452c33382e38352c3237322e33362c3132313131332c2c2c412a35327c30322e337c30312e337c30312e397c3030303030303030303030307c32303133313131323036313730387c31343034313430327c30303030303030307c30303245323137317c303030307c302e323137327c383930327c344639450d0a"))); + + } + +} |