From b5eb588704b76a4535e656253f3d50df1b046c68 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 13 Mar 2015 15:18:57 +1300 Subject: Reimplement BCE frame decoder --- src/org/traccar/protocol/BceFrameDecoder.java | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/org/traccar') 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; } } -- cgit v1.2.3