diff options
author | Ivan Muratov <binakot@gmail.com> | 2017-10-24 14:45:30 +0300 |
---|---|---|
committer | Ivan Muratov <binakot@gmail.com> | 2017-10-24 14:45:30 +0300 |
commit | d225018f7aadefab6381d5283347bae10e153bfa (patch) | |
tree | 7f7cfedc2ba6366b94d7ab0f926dbfab2dd4dae2 /src | |
parent | 05bcc7359b52cad0036bcb32888b7e999a3efb25 (diff) | |
download | trackermap-server-d225018f7aadefab6381d5283347bae10e153bfa.tar.gz trackermap-server-d225018f7aadefab6381d5283347bae10e153bfa.tar.bz2 trackermap-server-d225018f7aadefab6381d5283347bae10e153bfa.zip |
Arnavi4 frame decoder's code reorganization based on traccar maintainer comments.
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/protocol/Arnavi4FrameDecoder.java | 69 | ||||
-rw-r--r-- | src/org/traccar/protocol/Arnavi4ProtocolDecoder.java | 8 |
2 files changed, 37 insertions, 40 deletions
diff --git a/src/org/traccar/protocol/Arnavi4FrameDecoder.java b/src/org/traccar/protocol/Arnavi4FrameDecoder.java index 7c60cb934..b13f3fd7d 100644 --- a/src/org/traccar/protocol/Arnavi4FrameDecoder.java +++ b/src/org/traccar/protocol/Arnavi4FrameDecoder.java @@ -22,53 +22,50 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder; public class Arnavi4FrameDecoder extends FrameDecoder { - private static final int PACKET_MINIMUM_LENGTH = 4; - private static final int RECORD_MINIMUM_LENGTH = 8; - - static final byte HEADER_START_SIGN = (byte) 0xff; - static final byte HEADER_VERSION_1 = 0x22; - static final byte HEADER_VERSION_2 = 0x23; + private static final int MIN_LENGTH = 4; private static final int HEADER_LENGTH = 10; - - private static final byte PACKAGE_START_SIGN = 0x5b; + private static final int PACKET_WRAPPER_LENGTH = 8; + private static final int COMMAND_ANSWER_PACKET_LENGTH = 4; + private static final int COMMAND_ANSWER_PARCEL_NUMBER = 0xfd; private static final byte PACKAGE_END_SIGN = 0x5d; - private static final int PACKAGE_MIN_PARCEL_NUMBER = 0x01; - private static final int PACKAGE_MAX_PARCEL_NUMBER = 0xfb; + + private boolean firstPacket = true; @Override protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() < PACKET_MINIMUM_LENGTH) { + if (buf.readableBytes() < MIN_LENGTH) { return null; } - if (buf.getByte(0) == HEADER_START_SIGN - && buf.readableBytes() == HEADER_LENGTH - && (buf.getByte(1) == HEADER_VERSION_1 || buf.getByte(1) == HEADER_VERSION_2)) { - - return buf.readBytes(HEADER_LENGTH); - } - - int index = buf.getUnsignedByte(1); // parcel number - if (buf.getByte(0) == PACKAGE_START_SIGN - && index >= PACKAGE_MIN_PARCEL_NUMBER && index <= PACKAGE_MAX_PARCEL_NUMBER) { - - int bufferPosition = 2; // start sign + parcel number - while (bufferPosition + RECORD_MINIMUM_LENGTH < buf.readableBytes() - && buf.getByte(bufferPosition) != PACKAGE_END_SIGN) { - - int dataLength = buf.getUnsignedShort(bufferPosition + 1); - bufferPosition += RECORD_MINIMUM_LENGTH + dataLength; // type + data length + unixtime + data + crc + int length; + if (firstPacket) { + firstPacket = false; + length = HEADER_LENGTH; + } else { + int index = buf.getUnsignedByte(1); // parcel number + if (index == COMMAND_ANSWER_PARCEL_NUMBER) { + length = COMMAND_ANSWER_PACKET_LENGTH; + } else { + int pos = 2; // start sign + parcel number + while (pos + PACKET_WRAPPER_LENGTH < buf.readableBytes() + && buf.getByte(pos) != PACKAGE_END_SIGN) { + + int dataLength = buf.getUnsignedShort(pos + 1); + pos += PACKET_WRAPPER_LENGTH + dataLength; // packet type + data length + unixtime + data + crc + } + + if (buf.getByte(pos) != PACKAGE_END_SIGN) { // end sign + return null; + } + + length = pos + 1; } + } - if (bufferPosition < buf.readableBytes() - && buf.getByte(bufferPosition) == PACKAGE_END_SIGN) { - - return buf.readBytes(bufferPosition + 1); // end sign - } + if (buf.readableBytes() >= length) { + return buf.readBytes(length); } return null; diff --git a/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java b/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java index e3e027993..06abb563f 100644 --- a/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java +++ b/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java @@ -30,12 +30,12 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; -import static org.traccar.protocol.Arnavi4FrameDecoder.HEADER_START_SIGN; -import static org.traccar.protocol.Arnavi4FrameDecoder.HEADER_VERSION_1; -import static org.traccar.protocol.Arnavi4FrameDecoder.HEADER_VERSION_2; - public class Arnavi4ProtocolDecoder extends BaseProtocolDecoder { + private static final byte HEADER_START_SIGN = (byte) 0xff; + private static final byte HEADER_VERSION_1 = 0x22; + private static final byte HEADER_VERSION_2 = 0x23; + private static final byte RECORD_PING = 0x00; private static final byte RECORD_DATA = 0x01; private static final byte RECORD_TEXT = 0x03; |