diff options
author | Ivan Muratov <binakot@gmail.com> | 2017-07-13 08:42:17 +0300 |
---|---|---|
committer | Ivan Muratov <binakot@gmail.com> | 2017-07-13 08:42:17 +0300 |
commit | 358b591bc457786927ac1466e10181bbffbcf56b (patch) | |
tree | c55745c3ebf2c09acd2d784f828c79e2f3e588c6 | |
parent | 6e70a873886b366980423e448808b60c624e4047 (diff) | |
download | trackermap-server-358b591bc457786927ac1466e10181bbffbcf56b.tar.gz trackermap-server-358b591bc457786927ac1466e10181bbffbcf56b.tar.bz2 trackermap-server-358b591bc457786927ac1466e10181bbffbcf56b.zip |
Replace MODULO256 checksum method.
Added covering tests.
-rw-r--r-- | src/org/traccar/helper/Checksum.java | 8 | ||||
-rw-r--r-- | src/org/traccar/protocol/Arnavi4ProtocolDecoder.java | 11 | ||||
-rw-r--r-- | test/org/traccar/helper/ChecksumTest.java | 8 |
3 files changed, 18 insertions, 9 deletions
diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java index 43ba6a689..7a0151d36 100644 --- a/src/org/traccar/helper/Checksum.java +++ b/src/org/traccar/helper/Checksum.java @@ -243,4 +243,12 @@ public final class Checksum { return (10 - (checksum % 10)) % 10; } + public static int modulo256(byte... bytes) { + int sum = 0; + for (byte b : bytes) { + sum = (sum + b) & 0xFF; + } + return sum; + } + } diff --git a/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java b/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java index caa8dd28f..46caebb71 100644 --- a/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java +++ b/src/org/traccar/protocol/Arnavi4ProtocolDecoder.java @@ -5,6 +5,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.helper.Checksum; import org.traccar.model.Position; import java.net.SocketAddress; @@ -35,14 +36,6 @@ public class Arnavi4ProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static int modulo256Checksum(byte[] bytes) { - int sum = 0; - for (byte b : bytes) { - sum = (sum + b) & 0xFF; - } - return sum; - } - private Position decodePosition(DeviceSession deviceSession, ChannelBuffer buf, long timestamp) { final Position position = new Position(); @@ -107,7 +100,7 @@ public class Arnavi4ProtocolDecoder extends BaseProtocolDecoder { response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 9); response.writeBytes(new byte[]{0x7B, 0x04, 0x00}); byte[] timestampBytes = ByteBuffer.allocate(4).putInt((int) (System.currentTimeMillis() / 1000)).array(); - response.writeByte(modulo256Checksum(timestampBytes)); + response.writeByte(Checksum.modulo256(timestampBytes)); response.writeBytes(timestampBytes); response.writeByte(0x7D); diff --git a/test/org/traccar/helper/ChecksumTest.java b/test/org/traccar/helper/ChecksumTest.java index c37eda88d..737b65c62 100644 --- a/test/org/traccar/helper/ChecksumTest.java +++ b/test/org/traccar/helper/ChecksumTest.java @@ -4,6 +4,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.junit.Assert; import org.junit.Test; +import org.traccar.protocol.Arnavi4ProtocolDecoder; import java.nio.charset.StandardCharsets; @@ -28,4 +29,11 @@ public class ChecksumTest { Assert.assertEquals(0, Checksum.luhn(63070019470771L)); } + @Test + public void testModulo256() { + Assert.assertEquals(0x00, Checksum.modulo256((byte)0x00)); + Assert.assertEquals(0x00, Checksum.modulo256((byte)0x00, (byte)0x00, (byte)0x00)); + Assert.assertEquals(0x06, Checksum.modulo256((byte)0x01, (byte)0x02, (byte)0x03)); + } + } |