From 9cc1cbd2b21afcd40091069d2f0a58565db18c15 Mon Sep 17 00:00:00 2001 From: Vitaly Litvak Date: Sun, 2 Oct 2016 08:17:43 +0300 Subject: Moved 'xor' checksum method to the utility class. --- src/org/traccar/helper/Checksum.java | 9 +++++++++ src/org/traccar/protocol/Jt600ProtocolDecoder.java | 12 ++---------- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java index 2a4b1191d..dde174545 100644 --- a/src/org/traccar/helper/Checksum.java +++ b/src/org/traccar/helper/Checksum.java @@ -194,6 +194,15 @@ public final class Checksum { return checksum; } + public static int xor(String string) { + byte[] bytes = string.getBytes(StandardCharsets.US_ASCII); + byte sum = 0; + for (byte b : bytes) { + sum ^= b; + } + return sum; + } + public static String nmea(String msg) { int checksum = 0; for (byte b : msg.getBytes(StandardCharsets.US_ASCII)) { diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index 827d6fbb5..de4510d17 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -21,6 +21,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; +import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -267,7 +268,7 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { case "U02": case "U03": int checkSum = parser.nextInt(16); - int calculatedCheckSum = checkSum(sentence.substring(1, sentence.length() - 3)); + int calculatedCheckSum = Checksum.xor(sentence.substring(1, sentence.length() - 3)); if (checkSum == calculatedCheckSum) { sendResponse(channel, "(S39)"); return position; @@ -303,15 +304,6 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { return null; } - private byte checkSum(String sentence) { - byte[] bytes = sentence.getBytes(StandardCharsets.US_ASCII); - byte sum = 0; - for (byte b : bytes) { - sum ^= b; - } - return sum; - } - private void sendResponse(Channel channel, String response) { if (channel != null) { channel.write(ChannelBuffers.copiedBuffer(response, StandardCharsets.US_ASCII)); -- cgit v1.2.3