From 1570f3cda955c895498edafaa87b12aa4fdd6c33 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 5 Oct 2015 16:25:42 +1300 Subject: Fix checksum calculation methods --- src/org/traccar/helper/Crc.java | 20 ++++++++------------ src/org/traccar/protocol/Gt06ProtocolEncoder.java | 4 ++-- 2 files changed, 10 insertions(+), 14 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/helper/Crc.java b/src/org/traccar/helper/Crc.java index 7aa7d094a..3df6ffd71 100644 --- a/src/org/traccar/helper/Crc.java +++ b/src/org/traccar/helper/Crc.java @@ -99,21 +99,17 @@ public class Crc { private static int crc16Unreflected(ByteBuffer buf, int crc_in, int[] table) { int crc16 = crc_in; - - for (int i = 0; i < buf.remaining(); i++) { - crc16 = table[((crc16 >> 8) ^ buf.get(i)) & 0xff] ^ (crc16 << 8); + while (buf.hasRemaining()) { + crc16 = table[((crc16 >> 8) ^ buf.get()) & 0xff] ^ (crc16 << 8); } - return crc16 & 0xFFFF; } private static int crc16Reflected(ByteBuffer buf, int crc_in, int[] table) { int crc16 = crc_in; - - for (int i = 0; i < buf.remaining(); i++) { - crc16 = table[(crc16 ^ buf.get(i)) & 0xff] ^ (crc16 >> 8); + while (buf.hasRemaining()) { + crc16 = table[(crc16 ^ buf.get()) & 0xff] ^ (crc16 >> 8); } - return crc16 & 0xFFFF; } @@ -131,16 +127,16 @@ public class Crc { public static int crc32(ByteBuffer buf) { CRC32 checksum = new CRC32(); - for (int i = 0; i < buf.remaining(); i++) { - checksum.update(buf.get(i)); + while (buf.hasRemaining()) { + checksum.update(buf.get()); } return (int) checksum.getValue(); } public static int xorChecksum(ByteBuffer buf) { int checksum = 0; - for (int i = 0; i < buf.remaining(); i++) { - checksum ^= buf.get(i); + while (buf.hasRemaining()) { + checksum ^= buf.get(); } return checksum; } diff --git a/src/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/org/traccar/protocol/Gt06ProtocolEncoder.java index 495e2fff5..e842c3a69 100644 --- a/src/org/traccar/protocol/Gt06ProtocolEncoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolEncoder.java @@ -53,9 +53,9 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_ENGINE_STOP: - return encodeContent("RELAY,1#"); + return encodeContent("Relay,1#"); case Command.TYPE_ENGINE_RESUME: - return encodeContent("RELAY,0#"); + return encodeContent("Relay,0#"); } return null; -- cgit v1.2.3