From d53db01813ee67e7578f55c162639aa61608ed0b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 31 Jul 2013 20:56:53 +1200 Subject: Fix jt600 frame decoder (fix #290) --- src/org/traccar/protocol/Jt600FrameDecoder.java | 21 ++++++++++++-------- src/org/traccar/protocol/Jt600ProtocolDecoder.java | 23 +++------------------- 2 files changed, 16 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/Jt600FrameDecoder.java b/src/org/traccar/protocol/Jt600FrameDecoder.java index 2e2fd228b..d2fbc4ca8 100644 --- a/src/org/traccar/protocol/Jt600FrameDecoder.java +++ b/src/org/traccar/protocol/Jt600FrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,25 +24,30 @@ import org.traccar.helper.ChannelBufferTools; public class Jt600FrameDecoder extends FrameDecoder { - private static int MESSAGE_LENGTH = 37; - + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + // Check minimum length + int available = buf.readableBytes(); + if (available < 10) { + return null; + } + // Message identifier - char first = (char) buf.getByte(0); - int length = buf.readableBytes(); + char first = (char) buf.getByte(buf.readerIndex()); if (first == '$') { // Check length - if (length >= MESSAGE_LENGTH) { - return buf.readBytes(MESSAGE_LENGTH); + int length = buf.getUnsignedShort(buf.readerIndex() + 7) + 10; + if (length >= available) { + return buf.readBytes(length); } } else if (first == '(') { // Find ending - Integer endIndex = ChannelBufferTools.find(buf, 0, length, ")"); + Integer endIndex = ChannelBufferTools.find(buf, buf.readerIndex(), available, ")"); if (endIndex != null) { return buf.readBytes(endIndex + 1); } diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index 6a8f9da52..86711a644 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,21 +29,12 @@ import org.traccar.helper.ChannelBufferTools; import org.traccar.helper.Log; import org.traccar.model.Position; -/** - * JT600 protocol decoder - */ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { - /** - * Initialize - */ public Jt600ProtocolDecoder(ServerManager serverManager) { super(serverManager); } - /** - * Decode regular message - */ private Position decodeNormalMessage(ChannelBuffer buf) throws Exception { Position position = new Position(); @@ -56,7 +47,7 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { position.setDeviceId(getDataManager().getDeviceByImei(id).getId()); } catch(Exception error) { Log.warning("Unknown device - " + id); - return null; + //return null; } buf.readByte(); // protocol version + data type @@ -122,9 +113,6 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { return position; } - /** - * Alert message pattern - */ static private Pattern pattern = Pattern.compile( "\\(" + "([\\d]+)," + // Id @@ -144,9 +132,6 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { "(\\d+)," + // Alert Type ".*\\)"); - /** - * Decode alert message - */ private Position decodeAlertMessage(ChannelBuffer buf) throws Exception { String message = buf.toString(Charset.defaultCharset()); @@ -208,9 +193,7 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { return position; } - /** - * Decode message - */ + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { -- cgit v1.2.3