aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-10-06 16:55:31 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-10-06 16:55:31 +1300
commit2b9187ac736feeb661e2814af8fa534a335998d4 (patch)
tree18e89d33116194bc5f2fa90ea48b169608b81a76
parent6652e4c3e23da63d4277caaca7f91e651793f503 (diff)
downloadtrackermap-server-2b9187ac736feeb661e2814af8fa534a335998d4.tar.gz
trackermap-server-2b9187ac736feeb661e2814af8fa534a335998d4.tar.bz2
trackermap-server-2b9187ac736feeb661e2814af8fa534a335998d4.zip
Clean up CRC-16 implementations
-rw-r--r--src/org/traccar/helper/Checksum.java38
-rw-r--r--src/org/traccar/protocol/CastelProtocolDecoder.java4
-rw-r--r--src/org/traccar/protocol/CityeasyProtocolEncoder.java2
-rw-r--r--src/org/traccar/protocol/Gt06ProtocolDecoder.java2
-rw-r--r--src/org/traccar/protocol/Gt06ProtocolEncoder.java2
-rw-r--r--src/org/traccar/protocol/MeiligaoProtocolDecoder.java2
-rw-r--r--src/org/traccar/protocol/NavigilProtocolDecoder.java2
-rw-r--r--test/org/traccar/helper/ChecksumTest.java21
8 files changed, 51 insertions, 22 deletions
diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java
index cdf1bbf50..69c9dd499 100644
--- a/src/org/traccar/helper/Checksum.java
+++ b/src/org/traccar/helper/Checksum.java
@@ -91,8 +91,14 @@ public class Checksum {
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
- public static final int CRC16_CCITT = 0xFFFF;
- public static final int CRC16_CCITT_XOROUT = 0xFFFF;
+ // More info: http://reveng.sourceforge.net/crc-catalogue/16.htm
+ public static final String CRC16_X25 = "X-25";
+ public static final String CRC16_CCITT_FALSE = "CCITT-FALSE";
+ public static final String CRC16_KERMIT = "KERMIT";
+ public static final String CRC16_XMODEM = "XMODEM";
+ public static final String CRC16_AUG_CCITT = "AUG-CCITT";
+ public static final String CRC16_GENIBUS = "GENIBUS";
+ public static final String CRC16_MCRF4XX = "MCRF4XX";
private static int crc16Unreflected(ByteBuffer buf, int crc_in, int[] table) {
int crc16 = crc_in;
@@ -110,16 +116,24 @@ public class Checksum {
return crc16 & 0xFFFF;
}
- public static int crc16Ccitt(ByteBuffer buf) {
- return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE) ^ 0xFFFF;
- }
-
- public static int crc16X25Ccitt(ByteBuffer buf) {
- return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE);
- }
-
- public static int crc16Ccitt(ByteBuffer buf, int seed, int xorout) {
- return crc16Reflected(buf, seed, CRC16_CCITT_TABLE_REVERSE) ^ xorout;
+ public static int crc16(String type, ByteBuffer buf) {
+ switch (type) {
+ case CRC16_X25:
+ return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE) ^ 0xFFFF;
+ case CRC16_CCITT_FALSE:
+ return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE);
+ case CRC16_KERMIT:
+ return crc16Reflected(buf, 0, CRC16_CCITT_TABLE_REVERSE);
+ case CRC16_XMODEM:
+ return crc16Unreflected(buf, 0, CRC16_CCITT_TABLE);
+ case CRC16_AUG_CCITT:
+ return crc16Unreflected(buf, 0x1d0f, CRC16_CCITT_TABLE);
+ case CRC16_GENIBUS:
+ return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE) ^ 0xFFFF;
+ case CRC16_MCRF4XX:
+ return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE);
+ }
+ throw new UnsupportedOperationException();
}
public static int crc32(ByteBuffer buf) {
diff --git a/src/org/traccar/protocol/CastelProtocolDecoder.java b/src/org/traccar/protocol/CastelProtocolDecoder.java
index f93b9762c..d7c6bbf46 100644
--- a/src/org/traccar/protocol/CastelProtocolDecoder.java
+++ b/src/org/traccar/protocol/CastelProtocolDecoder.java
@@ -95,7 +95,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
response.writeByte(version);
response.writeBytes(id);
response.writeShort(ChannelBuffers.swapShort(MSG_HEARTBEAT_RESPONSE));
- response.writeShort(Checksum.crc16Ccitt(response.toByteBuffer(0, response.writerIndex())));
+ response.writeShort(Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex())));
response.writeByte(0x0D); response.writeByte(0x0A);
channel.write(response, remoteAddress);
}
@@ -123,7 +123,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
response.writeInt(0xFFFFFFFF);
response.writeShort(0);
response.writeInt((int) (System.currentTimeMillis() / 1000));
- response.writeShort(Checksum.crc16Ccitt(response.toByteBuffer(0, response.writerIndex())));
+ response.writeShort(Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex())));
response.writeByte(0x0D); response.writeByte(0x0A);
channel.write(response, remoteAddress);
}
diff --git a/src/org/traccar/protocol/CityeasyProtocolEncoder.java b/src/org/traccar/protocol/CityeasyProtocolEncoder.java
index cfe69a462..76c4199e1 100644
--- a/src/org/traccar/protocol/CityeasyProtocolEncoder.java
+++ b/src/org/traccar/protocol/CityeasyProtocolEncoder.java
@@ -33,7 +33,7 @@ public class CityeasyProtocolEncoder extends BaseProtocolEncoder {
buf.writeShort(type);
buf.writeBytes(content);
buf.writeInt(0x0B);
- buf.writeShort(Checksum.crc16Ccitt(buf.toByteBuffer(), 0, 0));
+ buf.writeShort(Checksum.crc16(Checksum.CRC16_KERMIT, buf.toByteBuffer()));
buf.writeByte('\r');
buf.writeByte('\n');
diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java
index ab5c80cf6..4c744dd9f 100644
--- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java
@@ -81,7 +81,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder {
response.writeByte(0x05); // size
response.writeByte(type);
response.writeShort(index);
- response.writeShort(Checksum.crc16Ccitt(response.toByteBuffer(2, 4)));
+ response.writeShort(Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(2, 4)));
response.writeByte(0x0D); response.writeByte(0x0A); // ending
channel.write(response);
}
diff --git a/src/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/org/traccar/protocol/Gt06ProtocolEncoder.java
index 9a1bb4571..f5b914b1c 100644
--- a/src/org/traccar/protocol/Gt06ProtocolEncoder.java
+++ b/src/org/traccar/protocol/Gt06ProtocolEncoder.java
@@ -40,7 +40,7 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder {
buf.writeShort(0); // message index
- buf.writeShort(Checksum.crc16Ccitt(buf.toByteBuffer(2, buf.writerIndex() - 2)));
+ buf.writeShort(Checksum.crc16(Checksum.CRC16_X25, buf.toByteBuffer(2, buf.writerIndex() - 2)));
buf.writeByte('\r');
buf.writeByte('\n');
diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
index bde28492f..a7d8a884e 100644
--- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
+++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
@@ -121,7 +121,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder {
buf.writeBytes(id);
buf.writeShort(type);
buf.writeBytes(msg);
- buf.writeShort(Checksum.crc16X25Ccitt(buf.toByteBuffer()));
+ buf.writeShort(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, buf.toByteBuffer()));
buf.writeByte('\r');
buf.writeByte('\n');
diff --git a/src/org/traccar/protocol/NavigilProtocolDecoder.java b/src/org/traccar/protocol/NavigilProtocolDecoder.java
index 24c6618bb..142d19fc6 100644
--- a/src/org/traccar/protocol/NavigilProtocolDecoder.java
+++ b/src/org/traccar/protocol/NavigilProtocolDecoder.java
@@ -68,7 +68,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder {
header.writeShort(MESSAGE_ACKNOWLEDGEMENT);
header.writeShort(header.capacity() + data.capacity());
header.writeShort(0);
- header.writeShort(Checksum.crc16X25Ccitt(data.toByteBuffer()));
+ header.writeShort(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, data.toByteBuffer()));
header.writeInt(0);
header.writeInt((int) (System.currentTimeMillis() / 1000) + LEAP_SECONDS_DELTA);
diff --git a/test/org/traccar/helper/ChecksumTest.java b/test/org/traccar/helper/ChecksumTest.java
index ad112b940..a2e69eb5a 100644
--- a/test/org/traccar/helper/ChecksumTest.java
+++ b/test/org/traccar/helper/ChecksumTest.java
@@ -1,16 +1,31 @@
package org.traccar.helper;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Assert;
import org.junit.Test;
+import java.nio.charset.Charset;
+
public class ChecksumTest {
-
+
@Test
- public void testLuhnChecksum() {
+ public void testCrc16() {
+ ChannelBuffer buf = ChannelBuffers.copiedBuffer("123456789", Charset.defaultCharset());
+ Assert.assertEquals(0x906e, Checksum.crc16(Checksum.CRC16_X25, buf.toByteBuffer()));
+ Assert.assertEquals(0x29b1, Checksum.crc16(Checksum.CRC16_CCITT_FALSE, buf.toByteBuffer()));
+ Assert.assertEquals(0x2189, Checksum.crc16(Checksum.CRC16_KERMIT, buf.toByteBuffer()));
+ Assert.assertEquals(0x31c3, Checksum.crc16(Checksum.CRC16_XMODEM, buf.toByteBuffer()));
+ Assert.assertEquals(0xe5cc, Checksum.crc16(Checksum.CRC16_AUG_CCITT, buf.toByteBuffer()));
+ Assert.assertEquals(0xd64e, Checksum.crc16(Checksum.CRC16_GENIBUS, buf.toByteBuffer()));
+ Assert.assertEquals(0x6f91, Checksum.crc16(Checksum.CRC16_MCRF4XX, buf.toByteBuffer()));
+ }
+
+ @Test
+ public void testLuhn() {
Assert.assertEquals(7, Checksum.luhn(12345678901234L));
Assert.assertEquals(0, Checksum.luhn(63070019470771L));
-
}
}