You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/03/15 10:05:58 UTC

[incubator-plc4x] branch master updated: implement crc by using external library

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/master by this push:
     new 983dd5c  implement crc by using external library
983dd5c is described below

commit 983dd5cbd4f5aee3302c12c733417b20b754e10e
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Mar 15 11:05:04 2018 +0100

    implement crc by using external library
---
 plc4j/protocols/ads/pom.xml                        |  6 +++++
 .../plc4x/java/ads/protocol/util/DigestUtil.java   | 28 ++++++++--------------
 2 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/plc4j/protocols/ads/pom.xml b/plc4j/protocols/ads/pom.xml
index ae5e6ad..bed02a3 100644
--- a/plc4j/protocols/ads/pom.xml
+++ b/plc4j/protocols/ads/pom.xml
@@ -80,6 +80,12 @@
     </dependency>
 
     <dependency>
+      <groupId>com.github.snksoft</groupId>
+      <artifactId>crc</artifactId>
+      <version>1.0.1</version>
+    </dependency>
+
+    <dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
     </dependency>
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
index ad74dc5..cfed7dd 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
@@ -18,20 +18,13 @@
  */
 package org.apache.plc4x.java.ads.protocol.util;
 
-import io.netty.buffer.ByteBuf;
+import com.github.snksoft.crc.CRC;
 import org.apache.plc4x.java.ads.api.util.ByteReadable;
 
-/**
- * TODO: temporary due to unclear licence
- * From https://stackoverflow.com/a/18333436/850036
- * // TODO: replace this with a better implementation. Maybe:
- * http://www.source-code.biz/snippets/java/crc16/
- * https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Standards_and_common_use
- * or even better a netty supplied crc-16
- * https://github.com/openhab/jamod/blob/64cdbd16fbb7febd39470f873a00be986b052e39/src/main/java/net/wimpi/modbus/util/ModbusUtil.java
- */
 public class DigestUtil {
 
+    private static CRC crc16 = new CRC(CRC.Parameters.CRC16);
+
     private DigestUtil() {
         // Utility class
     }
@@ -40,16 +33,15 @@ public class DigestUtil {
         if (byteReadables.length == 1) {
             return calculateCrc16(byteReadables[0].getBytes());
         }
-        return calculateCrc16(new ByteReadable() {
-            @Override
-            public ByteBuf getByteBuf() {
-                return buildByteBuff(byteReadables);
-            }
-        }.getBytes());
+        long currentCrcValue = crc16.init();
+        for (ByteReadable byteReadable : byteReadables) {
+            currentCrcValue = crc16.update(currentCrcValue, byteReadable.getBytes());
+        }
+        return crc16.finalCRC16(currentCrcValue);
     }
 
     public static int calculateCrc16(byte[] bytes) {
-        // TODO: implement me
-        return 0;
+        return (int) crc16.calculateCRC(bytes);
     }
+
 }

-- 
To stop receiving notification emails like this one, please contact
sruehl@apache.org.