aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-09-12 22:58:40 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-09-12 22:58:40 -0700
commite0f15c6a21c188edd61ca78d034aaf1505fb59cb (patch)
tree9d0936d30efabd5959204e54551fad90c7cffc18
parentab70f326d1ea09a20f2b5835d7de9d7ff6a5b220 (diff)
downloadtrackermap-server-e0f15c6a21c188edd61ca78d034aaf1505fb59cb.tar.gz
trackermap-server-e0f15c6a21c188edd61ca78d034aaf1505fb59cb.tar.bz2
trackermap-server-e0f15c6a21c188edd61ca78d034aaf1505fb59cb.zip
Refactor NMEA checksum
-rw-r--r--src/main/java/org/traccar/WebDataHandler.java2
-rw-r--r--src/main/java/org/traccar/helper/Checksum.java9
-rw-r--r--src/main/java/org/traccar/protocol/GpsGateProtocolDecoder.java3
-rw-r--r--src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java4
-rw-r--r--src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java2
-rw-r--r--src/test/java/org/traccar/helper/ChecksumTest.java5
6 files changed, 12 insertions, 13 deletions
diff --git a/src/main/java/org/traccar/WebDataHandler.java b/src/main/java/org/traccar/WebDataHandler.java
index 3be575b48..b3b83a9cf 100644
--- a/src/main/java/org/traccar/WebDataHandler.java
+++ b/src/main/java/org/traccar/WebDataHandler.java
@@ -117,7 +117,7 @@ public class WebDataHandler extends BaseDataHandler {
f.format("%1$td%1$tm%1$ty,,", calendar);
}
- s.append(Checksum.nmea(s.toString()));
+ s.append(Checksum.nmea(s.substring(1)));
return s.toString();
}
diff --git a/src/main/java/org/traccar/helper/Checksum.java b/src/main/java/org/traccar/helper/Checksum.java
index d41dc2992..c1de983d8 100644
--- a/src/main/java/org/traccar/helper/Checksum.java
+++ b/src/main/java/org/traccar/helper/Checksum.java
@@ -151,13 +151,8 @@ public final class Checksum {
return checksum;
}
- public static String nmea(String msg) {
- int checksum = 0;
- byte[] bytes = msg.getBytes(StandardCharsets.US_ASCII);
- for (int i = 1; i < bytes.length; i++) {
- checksum ^= bytes[i];
- }
- return String.format("*%02x", checksum).toUpperCase();
+ public static String nmea(String string) {
+ return String.format("*%02X", xor(string));
}
public static int sum(ByteBuffer buf) {
diff --git a/src/main/java/org/traccar/protocol/GpsGateProtocolDecoder.java b/src/main/java/org/traccar/protocol/GpsGateProtocolDecoder.java
index cc187225b..c158d3212 100644
--- a/src/main/java/org/traccar/protocol/GpsGateProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/GpsGateProtocolDecoder.java
@@ -69,7 +69,8 @@ public class GpsGateProtocolDecoder extends BaseProtocolDecoder {
private void send(Channel channel, SocketAddress remoteAddress, String message) {
if (channel != null) {
- channel.writeAndFlush(new NetworkMessage(message + Checksum.nmea(message) + "\r\n", remoteAddress));
+ channel.writeAndFlush(new NetworkMessage(
+ message + Checksum.nmea(message.substring(1)) + "\r\n", remoteAddress));
}
}
diff --git a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java
index 0c72568f3..4abb75025 100644
--- a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java
@@ -152,7 +152,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder {
if (responseCode != null) {
String response = "$AVCFG," + devicePassword + "," + responseCode;
- response += Checksum.nmea(response) + "\r\n";
+ response += Checksum.nmea(response.substring(1)) + "\r\n";
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
@@ -163,7 +163,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder {
if (Character.isLowerCase(status.charAt(0))) {
String response = "$EAVACK," + event + "," + checksum;
- response += Checksum.nmea(response) + "\r\n";
+ response += Checksum.nmea(response.substring(1)) + "\r\n";
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
diff --git a/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java b/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java
index 0c9f8ebb8..aaa5a70f7 100644
--- a/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java
+++ b/src/main/java/org/traccar/protocol/LaipacProtocolEncoder.java
@@ -29,7 +29,7 @@ public class LaipacProtocolEncoder extends StringProtocolEncoder {
@Override
protected String formatCommand(Command command, String format, String... keys) {
String sentence = super.formatCommand(command, "$" + format, keys);
- sentence += Checksum.nmea(sentence) + "\r\n";
+ sentence += Checksum.nmea(sentence.substring(1)) + "\r\n";
return sentence;
}
diff --git a/src/test/java/org/traccar/helper/ChecksumTest.java b/src/test/java/org/traccar/helper/ChecksumTest.java
index ff48527bc..248f4dcae 100644
--- a/src/test/java/org/traccar/helper/ChecksumTest.java
+++ b/src/test/java/org/traccar/helper/ChecksumTest.java
@@ -42,8 +42,11 @@ public class ChecksumTest {
assertEquals(0x00, Checksum.modulo256(ByteBuffer.wrap(new byte[] {0x00})));
assertEquals(0x00, Checksum.modulo256(ByteBuffer.wrap(new byte[] {0x00, 0x00, 0x00})));
assertEquals(0xca, Checksum.modulo256(ByteBuffer.wrap(new byte[] {0x77, 0x77, 0x77, 0x77, 0x77, 0x77})));
+ }
-
+ @Test
+ public void testNmea() {
+ assertEquals("*2A", Checksum.nmea("GSC,011412000010789,M4(Ro=500)"));
}
}