aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-03-13 15:18:57 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-03-13 15:18:57 +1300
commitb5eb588704b76a4535e656253f3d50df1b046c68 (patch)
treef148bbfc02b55ac366104cde68d15dee591e6e70 /src
parentcee735f6934c18b44b5529787224344aa3412ffa (diff)
downloadtrackermap-server-b5eb588704b76a4535e656253f3d50df1b046c68.tar.gz
trackermap-server-b5eb588704b76a4535e656253f3d50df1b046c68.tar.bz2
trackermap-server-b5eb588704b76a4535e656253f3d50df1b046c68.zip
Reimplement BCE frame decoder
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/protocol/BceFrameDecoder.java31
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;
}
}