]> git.basschouten.com Git - openhab-addons.git/blob
dad17e87a727126356ec773841b605156c26eebc
[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.sinope.internal.core.base;
14
15 import org.openhab.binding.sinope.internal.util.ByteUtil;
16 import org.openhab.binding.sinope.internal.util.CRC8;
17
18 /**
19  * The Class SinopeFrame.
20  *
21  * @author Pascal Larin - Initial contribution
22  */
23 abstract class SinopeFrame {
24
25     /** The Constant PREAMBLE. */
26     protected static final byte PREAMBLE = 0x55;
27
28     /** The Constant FRAME_CTL. */
29     protected static final byte FRAME_CTL = 0x00;
30
31     /** The Constant PREAMBLE_SIZE. */
32     protected static final int PREAMBLE_SIZE = 1;
33
34     /** The Constant FRAME_CTL_SIZE. */
35     protected static final int FRAME_CTL_SIZE = 1;
36
37     /** The Constant CRC_SIZE. */
38     protected static final int CRC_SIZE = 1;
39
40     /** The Constant SIZE_SIZE. */
41     protected static final int SIZE_SIZE = 2;
42
43     /** The Constant COMMAND_SIZE. */
44     protected static final byte COMMAND_SIZE = 2;
45
46     /** The crc 8. */
47     private final CRC8 crc8 = new CRC8();
48
49     /** The internal payload. */
50     protected byte[] internal_payload;
51
52     /**
53      * Gets the command.
54      *
55      * @return the command
56      */
57     protected abstract byte[] getCommand();
58
59     /**
60      * Gets the frame data.
61      *
62      * @return the frame data
63      */
64     protected abstract byte[] getFrameData();
65
66     /**
67      * Gets the payload.
68      *
69      * @return the payload
70      */
71     protected abstract byte[] getPayload();
72
73     /**
74      * Gets the crc8.
75      *
76      * @param buffer the buffer
77      * @return the crc8
78      */
79     protected byte getCRC8(byte[] buffer) {
80         crc8.reset();
81         crc8.update(buffer, 0, buffer.length - 1);
82         return (byte) (crc8.getValue());
83     }
84
85     /**
86      * @see java.lang.Object#toString()
87      */
88     /*
89      *
90      *
91      * @see java.lang.Object#toString()
92      */
93     @Override
94     public String toString() {
95         StringBuilder sb = new StringBuilder();
96         getPayload();
97         sb.append(ByteUtil.toString(internal_payload));
98         return sb.toString();
99     }
100
101     /**
102      * Sets the payload.
103      *
104      * @param payload the new payload
105      */
106     public void setPayload(byte[] payload) {
107         setInternal_payload(payload);
108     }
109
110     /**
111      * Gets the internal payload.
112      *
113      * @return the internal payload
114      */
115     protected byte[] getInternal_payload() {
116         return internal_payload;
117     }
118
119     /**
120      * Sets the internal payload.
121      *
122      * @param internal_payload the new internal payload
123      */
124     protected void setInternal_payload(byte[] internal_payload) {
125         this.internal_payload = internal_payload;
126     }
127 }