diff options
-rw-r--r-- | src/org/traccar/protocol/At2000FrameDecoder.java | 21 | ||||
-rw-r--r-- | src/org/traccar/protocol/At2000ProtocolDecoder.java | 15 | ||||
-rw-r--r-- | test/org/traccar/protocol/At2000ProtocolDecoderTest.java | 8 |
3 files changed, 29 insertions, 15 deletions
diff --git a/src/org/traccar/protocol/At2000FrameDecoder.java b/src/org/traccar/protocol/At2000FrameDecoder.java index d0be3f5f4..4c849afb7 100644 --- a/src/org/traccar/protocol/At2000FrameDecoder.java +++ b/src/org/traccar/protocol/At2000FrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,16 +16,31 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.frame.FrameDecoder; +import java.nio.ByteOrder; + public class At2000FrameDecoder extends FrameDecoder { private static final int BLOCK_LENGTH = 16; + private static final int ACK_LENGTH = 496; private boolean firstPacket = true; + private void sendResponse(Channel channel) { + if (channel != null) { + ChannelBuffer response = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 2 * BLOCK_LENGTH); + response.writeByte(At2000ProtocolDecoder.MSG_ACKNOWLEDGEMENT); + response.writeMedium(ChannelBuffers.swapMedium(1)); + response.writeByte(0x00); // success + response.writerIndex(2 * BLOCK_LENGTH); + channel.write(response); + } + } + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { @@ -47,6 +62,10 @@ public class At2000FrameDecoder extends FrameDecoder { length = (length / BLOCK_LENGTH + 1) * BLOCK_LENGTH; } + if (buf.readableBytes() >= length || buf.readableBytes() % ACK_LENGTH == 0) { + sendResponse(channel); + } + if (buf.readableBytes() >= length) { return buf.readBytes(length); } diff --git a/src/org/traccar/protocol/At2000ProtocolDecoder.java b/src/org/traccar/protocol/At2000ProtocolDecoder.java index 989e0d136..58b6f44e8 100644 --- a/src/org/traccar/protocol/At2000ProtocolDecoder.java +++ b/src/org/traccar/protocol/At2000ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,17 +48,6 @@ public class At2000ProtocolDecoder extends BaseProtocolDecoder { private Cipher cipher; - private static void sendResponse(Channel channel) { - if (channel != null) { - ChannelBuffer response = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 2 * BLOCK_LENGTH); - response.writeByte(MSG_ACKNOWLEDGEMENT); - response.writeMedium(ChannelBuffers.swapMedium(1)); - response.writeByte(0x00); // success - response.writerIndex(2 * BLOCK_LENGTH); - channel.write(response); - } - } - private static void sendRequest(Channel channel) { if (channel != null) { ChannelBuffer response = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, BLOCK_LENGTH); @@ -136,8 +125,6 @@ public class At2000ProtocolDecoder extends BaseProtocolDecoder { } - sendResponse(channel); - if (type == MSG_DEVICE_ID) { sendRequest(channel); } diff --git a/test/org/traccar/protocol/At2000ProtocolDecoderTest.java b/test/org/traccar/protocol/At2000ProtocolDecoderTest.java index 6df44eaed..60485c805 100644 --- a/test/org/traccar/protocol/At2000ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/At2000ProtocolDecoderTest.java @@ -19,6 +19,14 @@ public class At2000ProtocolDecoderTest extends ProtocolTest { decoder = new At2000ProtocolDecoder(new At2000Protocol()); verifyNothing(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "01012f000000000000000000000000000033353734353430373136303631393637f5441a9862260117858237fe3160388490f0df7d46c09112ee087235a92101")); + + verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "89043203000000000000000000000000d01ff1df1b56ba9185bb741ddc253f3cee093b1f8193100cd95777b5288a6f29d1b343a952f882ce8825679f7e27ad88ed7898bff92f716acadfc3c17fa8c1a6b9d0934e8f042433a183776c06c1acd73efb4b9f19030debb4dceb161fb3e6630757d25c3e995b7cf5f446318dcc1677eb215d1af49f11cd7300598bcdc40cc25466ed2391d836c782e44bc04a332e902b2b34f5597a542af4ca670cdfc18d87ce2a225c3e6f2f32359d4914c6df09aa5ee306c229260d4a56da53f93398bc8a6e77095305ee214cf605de20d3876a993fb810486f75bcd514c12442bf4dc3fbe7963b20d5100b5ecff1c1aef4c4b3736a04e245d50f538327db21d55270b279db5ac5a9658876bae3d9b5026b8975bb2bf4d100b8492760d66ae31f27bf9c525c2d794860eabca9c788b91152dbce79f336daaf6a7a9547bf1dd8e3334c891f4548fd6d112ebf45125c2a8abd3a786ebbcfdd03101b524bbf465f14a9a424305ce7de56ffca85b4657fc8c03e4349c0ca6be64d1cf595ee91f8173678ef2267dae54dd00028450c48d9b74c925af0f245d409d8773238dce5832747587f53a12155869c1d464eb0630f94cf8dceb76aa39995411d4ce7743b1501692425afec498535526067e79f568b7f71ee47d8b4929118d57b13d56cdbfd26582d579dee")); + + decoder = new At2000ProtocolDecoder(new At2000Protocol()); + + verifyNothing(decoder, binary(ByteOrder.LITTLE_ENDIAN, "01012f0000000000000000000000000000333537343534303731363035353033dd529a1eb5df9f3b6d320b38250e03306692957e8c2127d8e381a717f639b4c9")); verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, |