diff options
author | Evgeny S Maksimov <me@vragam.net> | 2019-01-28 23:44:12 +0300 |
---|---|---|
committer | Evgeny S Maksimov <me@vragam.net> | 2019-01-28 23:44:12 +0300 |
commit | cbddcd311a777f0fbe1dcea4105b01ef0ffa4e34 (patch) | |
tree | 8d40a5358840b386f0a1f0b2d6110c9893c676f4 | |
parent | 95285d6f8c93d4a9c8e03d634b0a503695fe4449 (diff) | |
download | traccar-server-cbddcd311a777f0fbe1dcea4105b01ef0ffa4e34.tar.gz traccar-server-cbddcd311a777f0fbe1dcea4105b01ef0ffa4e34.tar.bz2 traccar-server-cbddcd311a777f0fbe1dcea4105b01ef0ffa4e34.zip |
Message type parser refactoring
-rw-r--r-- | src/org/traccar/protocol/NavisProtocolDecoder.java | 137 | ||||
-rw-r--r-- | test/org/traccar/protocol/NavisProtocolDecoderTest.java | 9 |
2 files changed, 82 insertions, 64 deletions
diff --git a/src/org/traccar/protocol/NavisProtocolDecoder.java b/src/org/traccar/protocol/NavisProtocolDecoder.java index 65c425053..decfae63f 100644 --- a/src/org/traccar/protocol/NavisProtocolDecoder.java +++ b/src/org/traccar/protocol/NavisProtocolDecoder.java @@ -649,82 +649,97 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { } } - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + private Object decodeNtcb(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { - ByteBuf buf = (ByteBuf) msg; + prefix = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII); + buf.skipBytes(prefix.length()); // Prefix @NTC by default + serverId = buf.readUnsignedIntLE(); + deviceUniqueId = buf.readUnsignedIntLE(); + int length = buf.readUnsignedShortLE(); + buf.skipBytes(2); // Header and data XOR checksum - if (buf.getByte(buf.readerIndex()) == 0x7F) { - // FLEX keep alive message - return null; - } else if (buf.getByte(buf.readerIndex()) == 0x7E) { // "~" - // FLEX message - try { - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession != null) { - switch (buf.readShortLE()) { - // FLEX 1.0 - case 0x417E: // "~A" - return processFlexArray(this::parseFlexPosition, "~A", deviceSession, channel, buf); - case 0x547E: // "~T" - return processFlexSingle(this::parseFlexPosition, "~T", deviceSession, channel, buf); - case 0x437E: // "~C" - return processFlexSingle(this::parseFlexPosition, "~C", deviceSession, channel, buf); - // FLEX 2.0 (Extra packages) - case 0x457E: // "~E" - return processFlexArray(this::parseFlex20Position, "~E", deviceSession, channel, buf); - case 0x587E: // "~X" - return processFlexSingle(this::parseFlex20Position, "~X", deviceSession, channel, buf); + if (length == 0) { + return null; // Keep alive message + } + + String type = buf.toString(buf.readerIndex(), 3, StandardCharsets.US_ASCII); + buf.skipBytes(type.length()); + + if (type.equals("*>S")) { + return processHandshake(channel, remoteAddress, buf); + } else { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + try { + switch (type) { + case "*>A": + return processNtcbArray(deviceSession, channel, buf); + case "*>T": + return processNtcbSingle(deviceSession, channel, buf); + case "*>F": // "*>FLEX" + buf.skipBytes(3); + return processFlexNegotiation(channel, buf); default: break; } + } catch (IndexOutOfBoundsException error) { + LOGGER.warn("Navis NTCB message parsing error", error); } - } catch (IndexOutOfBoundsException error) { - LOGGER.warn("Navis FLEX message parsing error", error); - } - } else { - // NTCB message - prefix = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII); - buf.skipBytes(prefix.length()); // Prefix @NTC by default - serverId = buf.readUnsignedIntLE(); - deviceUniqueId = buf.readUnsignedIntLE(); - int length = buf.readUnsignedShortLE(); - buf.skipBytes(2); // Header and data XOR checksum - - if (length == 0) { - return null; // Keep alive message } + } - int type = buf.getIntLE(buf.readerIndex()); - buf.skipBytes(3); - if ((type & 0xFFFFFF) == 0x533E2AL) { // "*>S" - return processHandshake(channel, remoteAddress, buf); - } else { - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession != null) { - try { - switch (type & 0xFFFFFF) { - case 0x413E2A: // "*>A" - return processNtcbArray(deviceSession, channel, buf); - case 0x543E2A: // "*>T" - return processNtcbSingle(deviceSession, channel, buf); - case 0x463E2A: // "*>F" (*>FLEX) - buf.skipBytes(3); - return processFlexNegotiation(channel, buf); - default: - break; - } - } catch (IndexOutOfBoundsException error) { - LOGGER.warn("Navis NTCB message parsing error", error); - } + return null; + } + + private Object decodeFlex(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { + + String type = buf.toString(buf.readerIndex(), 2, StandardCharsets.US_ASCII); + buf.skipBytes(type.length()); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + try { + switch (type) { + // FLEX 1.0 + case "~A": + return processFlexArray(this::parseFlexPosition, type, deviceSession, channel, buf); + case "~T": + case "~C": + return processFlexSingle(this::parseFlexPosition, type, deviceSession, channel, buf); + // FLEX 2.0 (Extra packages) + case "~E": + return processFlexArray(this::parseFlex20Position, type, deviceSession, channel, buf); + case "~X": + return processFlexSingle(this::parseFlex20Position, type, deviceSession, channel, buf); + default: + break; } + } catch (IndexOutOfBoundsException error) { + LOGGER.warn("Navis FLEX message parsing error", error); } } return null; } + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ByteBuf buf = (ByteBuf) msg; + + if (buf.getByte(buf.readerIndex()) == 0x7F) { + // FLEX keep alive message + return null; + } + + if (flexDataSize > 0) { + return decodeFlex(channel, remoteAddress, buf); + } else { + return decodeNtcb(channel, remoteAddress, buf); + } + } + public int getFlexDataSize() { return flexDataSize; } diff --git a/test/org/traccar/protocol/NavisProtocolDecoderTest.java b/test/org/traccar/protocol/NavisProtocolDecoderTest.java index 7230bc0a1..5bf0c84bc 100644 --- a/test/org/traccar/protocol/NavisProtocolDecoderTest.java +++ b/test/org/traccar/protocol/NavisProtocolDecoderTest.java @@ -15,9 +15,6 @@ public class NavisProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, binary( "404E5443010000007B000000130044342A3E533A383631373835303035323035303739")); - verifyNull(decoder, binary( - "404E5443010000007B000000130047372A3E533A383631373835303035313236303639")); - // NTCB F5.2 - Single verifyPosition(decoder, binary( "404e5443010000000000000059009adb2a3e54250000000000ff1500040b0a1008291838001200760ee600000000000000000000000f1500040b0a10ac20703fb1aec23f00000000320149668f430000000000000000000000000000000000000000000000f3808080"), @@ -55,6 +52,12 @@ public class NavisProtocolDecoderTest extends ProtocolTest { verifyPositions(decoder, binary( "7e4101080000000917c057405c002b001833c057405cbbce030225129101a00300007c6102408900400c1b3cfce3b23a12004710e000000000001bff7f000080bfffff80000080bfffffffffb2")); + decoder = new NavisProtocolDecoder(null); + + // Handshake + verifyNull(decoder, binary( + "404E5443010000007B000000130047372A3E533A383631373835303035313236303639")); + // FLEX 2.0 - Negotiation verifyNull(decoder, binary( "404e544301000000a9eef6021a003f8e2a3e464c4558b014147afffff008080800000e00000000000000")); |