diff options
author | Evgeny S Maksimov <admin@vragam.net> | 2019-01-18 21:48:41 +0300 |
---|---|---|
committer | Evgeny S Maksimov <admin@vragam.net> | 2019-01-18 21:48:41 +0300 |
commit | cbe838ad644ce5e787f080e292bc332e50efa4a0 (patch) | |
tree | 0c09249dc18b16fe2803b4d4a42670141f13e4ef /src/org/traccar/protocol/NavisFrameDecoder.java | |
parent | fc707a69c3c95223ecb8e263d7de32a72ec84af3 (diff) | |
download | trackermap-server-cbe838ad644ce5e787f080e292bc332e50efa4a0.tar.gz trackermap-server-cbe838ad644ce5e787f080e292bc332e50efa4a0.tar.bz2 trackermap-server-cbe838ad644ce5e787f080e292bc332e50efa4a0.zip |
Added strict implementation for frame decoder
Diffstat (limited to 'src/org/traccar/protocol/NavisFrameDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/NavisFrameDecoder.java | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/src/org/traccar/protocol/NavisFrameDecoder.java b/src/org/traccar/protocol/NavisFrameDecoder.java index 2480629ac..aa057b51f 100644 --- a/src/org/traccar/protocol/NavisFrameDecoder.java +++ b/src/org/traccar/protocol/NavisFrameDecoder.java @@ -25,17 +25,13 @@ public class NavisFrameDecoder extends BaseFrameDecoder { private static final int NTCB_HEADER_LENGHT = 16; private static final int NTCB_LENGHT_OFFSET = 12; + private static final int FLEX_HEADER_LENGHT = 2; private static final int MAX_FRAME_LENGHT = 65551; - private static byte src8Checksum(ByteBuf buf, int length) { - byte sum = (byte) 0xFF; - for (int i = 0; i < length; i++) { - sum ^= buf.getUnsignedByte(i); - for (int j = 0; j < 8; j++) { - sum = (sum & 0x80) != 0 ? (byte) ((sum << 1) ^ 0x31) : (byte) (sum << 1); - } - } - return sum; + private final NavisProtocolDecoder protocolDecoder; + + public NavisFrameDecoder(NavisProtocolDecoder protocolDecoder) { + this.protocolDecoder = protocolDecoder; } @Override @@ -52,12 +48,49 @@ public class NavisFrameDecoder extends BaseFrameDecoder { return buf.readRetainedSlice(1); } else if (buf.getByte(buf.readerIndex()) == 0x7E /* "~" */) { // FLEX frame + if (protocolDecoder.getFLEXDataSize() == 0) { + return null; + } if (buf.readableBytes() > MAX_FRAME_LENGHT) { throw new TooLongFrameException(); } - if (src8Checksum(buf, buf.readableBytes() - 1) == buf.getByte(buf.readableBytes() - 1)) { - return buf.readRetainedSlice(buf.readableBytes()); + if (buf.readableBytes() > FLEX_HEADER_LENGHT) { + int length = 0; + switch (buf.getByte(buf.readerIndex() + 1)) { + // FLEX 1.0 + case 0x41: // "A" + length = protocolDecoder.getFLEXDataSize() + * buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGHT) + 2; + break; + case 0x54: // "T" + length = protocolDecoder.getFLEXDataSize() + 5; + break; + case 0x43: // "C" + length = protocolDecoder.getFLEXDataSize() + 1; + break; + // FLEX 2.0 (Extra packages) + case 0x45: // "E" + length++; + for (int i = 0; i < buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGHT); i++) { + if (buf.readableBytes() > FLEX_HEADER_LENGHT + length + 1) { + length += buf.getUnsignedShort(length + FLEX_HEADER_LENGHT) + 2; + } else { + return null; + } + } + length++; + break; + case 0x58: // "X" + length = buf.getUnsignedShortLE(buf.readerIndex() + FLEX_HEADER_LENGHT) + 7; + break; + default: + break; + } + + if (buf.readableBytes() >= FLEX_HEADER_LENGHT + length) { + return buf.readRetainedSlice(buf.readableBytes()); + } } } else { // NTCB frame |