diff options
Diffstat (limited to 'src/org/traccar/protocol/BceFrameDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/BceFrameDecoder.java | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/org/traccar/protocol/BceFrameDecoder.java b/src/org/traccar/protocol/BceFrameDecoder.java index 2bb249348..8accc02d8 100644 --- a/src/org/traccar/protocol/BceFrameDecoder.java +++ b/src/org/traccar/protocol/BceFrameDecoder.java @@ -18,30 +18,45 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; -public class BceFrameDecoder extends LengthFieldBasedFrameDecoder { +public class BceFrameDecoder extends FrameDecoder { private static final int HANDSHAKE_LENGTH = 7; // "#BCE#\r\n" - + private boolean header = true; - - public BceFrameDecoder() { - super(1024, 8, 2, 8 + 2 + 1, 0); + + private static byte checksum(ChannelBuffer buf, int end) { + byte result = 0; + for (int i = 0; i < end; i++) { + result += buf.getByte(buf.readerIndex() + i); + } + return result; } - + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - + if (header && buf.readableBytes() >= HANDSHAKE_LENGTH) { buf.skipBytes(HANDSHAKE_LENGTH); header = false; } - return super.decode(ctx, channel, buf); + int end = 8; // IMEI + + while (buf.readableBytes() >= end + 2 + 1 + 1 + 1) { + end += buf.getUnsignedShort(buf.readerIndex() + end) + 2; + + if (buf.readableBytes() > end && checksum(buf, end) == buf.getByte(buf.readerIndex() + end)) { + return buf.readBytes(end + 1); + } + } + + return null; } } |