2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pentair.internal;
16 * Generic class for the standard pentair package protocol. Includes helpers to generate checksum and extract key bytes
19 * @author Jeff James - initial contribution
22 public class PentairPacket {
23 protected static final char[] HEXARRAY = "0123456789ABCDEF".toCharArray();
25 public static final int OFFSET = 2;
26 public static final int DEST = 0 + OFFSET;
27 public static final int SOURCE = 1 + OFFSET;
28 public static final int ACTION = 2 + OFFSET;
29 public static final int LENGTH = 3 + OFFSET;
30 public static final int STARTOFDATA = 4 + OFFSET;
32 protected boolean initialized;
37 * Constructor for PentairPacket basic packet.
39 public PentairPacket() {
46 * Constructor for a PentairPackage with a byte array for the command. Typically used when generating a packet to
47 * send. Should include all bytes starting with A5. Do not include checksum bytes.
49 * @param buf Array of bytes to be used to populate packet.
51 public PentairPacket(byte[] buf) {
58 * Constructor to create a copy of PentairPacket p. Note references the same byte array as original. Used when
59 * coverting from a generic packet to a specialized packet.
61 * @param p PentairPacket to duplicate in new copy.
63 public PentairPacket(PentairPacket p) {
70 * Gets length of packet
72 * @return length of packet
74 public int getLength() {
79 * Sets length of packet
81 * @param length length of packet
83 public void setLength(int length) {
84 if (length > buf[LENGTH]) {
85 buf = new byte[length + 6];
87 buf[LENGTH] = (byte) length;
91 * Gets action byte of packet
93 * @return action byte of packet
95 public int getAction() {
100 * Sets action byte of packet
104 public void setAction(int action) {
105 buf[ACTION] = (byte) action;
109 * Gets source byte or packet
111 * @return source byte of packet
113 public int getSource() {
118 * Sets source byte of packet
120 * @param source sets source byte of packet
122 public void setSource(int source) {
123 buf[SOURCE] = (byte) source;
127 * Gets destination byte of packet
129 * @return destination byte of packet
131 public int getDest() {
136 * Sets destination byte of packet
138 * @param dest destination byte of packet
140 public void setDest(int dest) {
141 buf[DEST] = (byte) dest;
145 * Helper function to convert byte to hex representation
147 * @param b byte to re
148 * @return 2 charater hex string representing the byte
150 public static String byteToHex(int b) {
151 char[] hexChars = new char[2];
153 hexChars[0] = HEXARRAY[b >>> 4];
154 hexChars[1] = HEXARRAY[b & 0x0F];
156 return new String(hexChars);
160 * @param bytes array of bytes to convert to a hex string. Entire buf length is converted.
163 public static String bytesToHex(byte[] bytes) {
164 return bytesToHex(bytes, bytes.length);
168 * @param bytes array of bytes to convert to a hex string.
169 * @param len Number of bytes to convert
172 public static String bytesToHex(byte[] bytes, int len) {
173 char[] hexChars = new char[len * 3];
174 for (int j = 0; j < len; j++) {
175 int v = bytes[j] & 0xFF;
176 hexChars[j * 3] = HEXARRAY[v >>> 4];
177 hexChars[j * 3 + 1] = HEXARRAY[v & 0x0F];
178 hexChars[j * 3 + 2] = ' ';
180 return new String(hexChars);
186 * @see java.lang.Object#toString()
189 public String toString() {
190 return bytesToHex(buf, getLength() + 6);
194 * Used to extract a specific byte from the packet
196 * @param n number of byte (0 based)
197 * @return byte of packet
199 public int getByte(int n) {
204 * Calculate checksum of the representative packet.
206 * @return checksum of packet
208 public int calcChecksum() {
211 for (i = 0; i < getLength() + 6; i++) {
212 checksum += buf[i] & 0xFF;