]> git.basschouten.com Git - openhab-addons.git/blob
487f0ed391d235af4417f9fbd1f49bb91e9abe9a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.fineoffsetweatherstation.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * Utility class.
19  *
20  * @author Andreas Berger - Initial contribution
21  */
22 @NonNullByDefault
23 public class Utils {
24
25     public static String toHexString(byte[] hex, int length, String delimiter) {
26         String[] hexadecimal = new String[length];
27         for (int i = 0; i < length; i++) {
28             hexadecimal[i] = String.format("%02X", hex[i]);
29         }
30         return String.join(delimiter, hexadecimal);
31     }
32
33     public static boolean validateChecksum(byte[] data, int sizeBytes) {
34         int size;
35
36         // general response
37         // | 1 byte size | 2 byte size
38         // | -----------------------------|--------------------
39         // | 0 - 0xff - header | 0 - 0xff - header
40         // | 1 - 0xff | 1 - 0xff
41         // | 2 - command | 2 - command
42         // | 3 - total size of response | 3 - size1
43         // | 4-X - data | 4 - size2
44         // | X+1 - checksum | 5-X - data
45         // | | X+1 - checksum
46
47         if (sizeBytes == 1) {
48             size = Utils.toUInt8(data[3]);
49         } else {
50             size = toUInt16(data, 3);
51         }
52
53         byte checksum = sum(data, 2, size);
54         return checksum == data[size + 1];
55     }
56
57     private static byte sum(byte[] data, int start, int end) {
58         byte checksum = 0;
59         for (var i = start; i <= end; i++) {
60             checksum += data[i];
61         }
62         return checksum;
63     }
64
65     public static int toUInt8(byte data) {
66         return Byte.toUnsignedInt(data);
67     }
68
69     public static int toInt16(byte[] array, int start) {
70         int result = ((int) array[start]) << 24;
71         result |= Utils.toUInt8(array[start + 1]) << 16;
72         return result >> 16;
73     }
74
75     public static int toUInt16(byte[] array, int start) {
76         return (Utils.toUInt8(array[start]) << 8 | Utils.toUInt8(array[start + 1]));
77     }
78
79     public static int toUInt32(byte[] array, int start) {
80         return (Utils.toUInt8(array[start++]) << 24 | Utils.toUInt8(array[start++]) << 16
81                 | Utils.toUInt8(array[start++]) << 8 | Utils.toUInt8(array[start]));
82     }
83
84     public static long toUInt64(byte[] array, int start) {
85         return ((long) Utils.toUInt8(array[start++]) << 24 | (long) toUInt8(array[start++]) << 16
86                 | (long) toUInt8(array[start++]) << 8 | Utils.toUInt8(array[start]));
87     }
88 }