]> git.basschouten.com Git - openhab-addons.git/blob
0462ba45e3b505ebe14e65acb93268da3d372bde
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.pentair.internal;
14
15 /**
16  * Generic class for the standard pentair package protocol. Includes helpers to generate checksum and extract key bytes
17  * from packet.
18  *
19  * @author Jeff James - initial contribution
20  *
21  */
22 public class PentairPacket {
23     protected static final char[] HEXARRAY = "0123456789ABCDEF".toCharArray();
24
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;
31
32     protected boolean initialized;
33
34     public byte[] buf;
35
36     /**
37      * Constructor for PentairPacket basic packet.
38      */
39     public PentairPacket() {
40         buf = new byte[6];
41
42         buf[0] = (byte) 0xA5;
43     }
44
45     /**
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.
48      *
49      * @param buf Array of bytes to be used to populate packet.
50      */
51     public PentairPacket(byte[] buf) {
52         this.buf = buf;
53
54         initialized = true;
55     }
56
57     /**
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.
60      *
61      * @param p PentairPacket to duplicate in new copy.
62      */
63     public PentairPacket(PentairPacket p) {
64         this.buf = p.buf;
65
66         initialized = true;
67     }
68
69     /**
70      * Gets length of packet
71      *
72      * @return length of packet
73      */
74     public int getLength() {
75         return buf[LENGTH];
76     }
77
78     /**
79      * Sets length of packet
80      *
81      * @param length length of packet
82      */
83     public void setLength(int length) {
84         if (length > buf[LENGTH]) {
85             buf = new byte[length + 6];
86         }
87         buf[LENGTH] = (byte) length;
88     }
89
90     /**
91      * Gets action byte of packet
92      *
93      * @return action byte of packet
94      */
95     public int getAction() {
96         return buf[ACTION];
97     }
98
99     /**
100      * Sets action byte of packet
101      *
102      * @param action
103      */
104     public void setAction(int action) {
105         buf[ACTION] = (byte) action;
106     }
107
108     /**
109      * Gets source byte or packet
110      *
111      * @return source byte of packet
112      */
113     public int getSource() {
114         return buf[SOURCE];
115     }
116
117     /**
118      * Sets source byte of packet
119      *
120      * @param source sets source byte of packet
121      */
122     public void setSource(int source) {
123         buf[SOURCE] = (byte) source;
124     }
125
126     /**
127      * Gets destination byte of packet
128      *
129      * @return destination byte of packet
130      */
131     public int getDest() {
132         return buf[DEST];
133     }
134
135     /**
136      * Sets destination byte of packet
137      *
138      * @param dest destination byte of packet
139      */
140     public void setDest(int dest) {
141         buf[DEST] = (byte) dest;
142     }
143
144     /**
145      * Helper function to convert byte to hex representation
146      *
147      * @param b byte to re
148      * @return 2 charater hex string representing the byte
149      */
150     public static String byteToHex(int b) {
151         char[] hexChars = new char[2];
152
153         hexChars[0] = HEXARRAY[b >>> 4];
154         hexChars[1] = HEXARRAY[b & 0x0F];
155
156         return new String(hexChars);
157     }
158
159     /**
160      * @param bytes array of bytes to convert to a hex string. Entire buf length is converted.
161      * @return hex string
162      */
163     public static String bytesToHex(byte[] bytes) {
164         return bytesToHex(bytes, bytes.length);
165     }
166
167     /**
168      * @param bytes array of bytes to convert to a hex string.
169      * @param len Number of bytes to convert
170      * @return hex string
171      */
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] = ' ';
179         }
180         return new String(hexChars);
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see java.lang.Object#toString()
187      */
188     @Override
189     public String toString() {
190         return bytesToHex(buf, getLength() + 6);
191     }
192
193     /**
194      * Used to extract a specific byte from the packet
195      *
196      * @param n number of byte (0 based)
197      * @return byte of packet
198      */
199     public int getByte(int n) {
200         return buf[n];
201     }
202
203     /**
204      * Calculate checksum of the representative packet.
205      *
206      * @return checksum of packet
207      */
208     public int calcChecksum() {
209         int checksum = 0, i;
210
211         for (i = 0; i < getLength() + 6; i++) {
212             checksum += buf[i] & 0xFF;
213         }
214
215         return checksum;
216     }
217 }