]> git.basschouten.com Git - openhab-addons.git/blob
a2f58c0ceff2b9c0e9a5636889be9cf396fd3e93
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nikobus.internal.utils;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.openhab.core.util.HexUtils;
17
18 /**
19  * The {@link CRCUtil} class defines utility functions to calculate CRC used by the Nikobus communication protocol.
20  *
21  * @author Davy Vanherbergen - Initial contribution
22  * @author Boris Krivonog - Removed dependency to javax.xml.bind.DatatypeConverter
23  */
24 public class CRCUtil {
25
26     private static final int CRC_INIT = 0xFFFF;
27
28     private static final int POLYNOMIAL = 0x1021;
29
30     /**
31      * Calculate the CRC16-CCITT checksum on the input string and return the
32      * input string with the checksum appended.
33      *
34      * @param input
35      *            String representing hex numbers.
36      * @return input string + CRC.
37      */
38     public static String appendCRC(String input) {
39         if (input == null) {
40             return null;
41         }
42
43         int check = CRC_INIT;
44
45         for (byte b : HexUtils.hexToBytes(input)) {
46             for (int i = 0; i < 8; i++) {
47                 if (((b >> (7 - i) & 1) == 1) ^ ((check >> 15 & 1) == 1)) {
48                     check = check << 1;
49                     check = check ^ POLYNOMIAL;
50                 } else {
51                     check = check << 1;
52                 }
53             }
54         }
55
56         check = check & CRC_INIT;
57         String checksum = StringUtils.leftPad(Integer.toHexString(check), 4, "0");
58         return (input + checksum).toUpperCase();
59     }
60
61     /**
62      * Calculate the second checksum on the input string and return the
63      * input string with the checksum appended.
64      *
65      * @param input
66      *            String representing a nikobus command.
67      * @return input string + CRC.
68      */
69     public static String appendCRC2(String input) {
70         int check = 0;
71
72         for (byte b : input.getBytes()) {
73
74             check = check ^ b;
75
76             for (int i = 0; i < 8; i++) {
77
78                 if (((check & 0xff) >> 7) != 0) {
79                     check = check << 1;
80                     check = check ^ 0x99;
81                 } else {
82                     check = check << 1;
83                 }
84                 check = check & 0xff;
85             }
86         }
87
88         return input + StringUtils.leftPad(Integer.toHexString(check), 2, "0").toUpperCase();
89     }
90 }