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.sinope.internal.core.base;
15 import org.openhab.binding.sinope.internal.util.ByteUtil;
16 import org.openhab.binding.sinope.internal.util.CRC8;
19 * The Class SinopeFrame.
21 * @author Pascal Larin - Initial contribution
23 abstract class SinopeFrame {
25 /** The Constant PREAMBLE. */
26 protected static final byte PREAMBLE = 0x55;
28 /** The Constant FRAME_CTL. */
29 protected static final byte FRAME_CTL = 0x00;
31 /** The Constant PREAMBLE_SIZE. */
32 protected static final int PREAMBLE_SIZE = 1;
34 /** The Constant FRAME_CTL_SIZE. */
35 protected static final int FRAME_CTL_SIZE = 1;
37 /** The Constant CRC_SIZE. */
38 protected static final int CRC_SIZE = 1;
40 /** The Constant SIZE_SIZE. */
41 protected static final int SIZE_SIZE = 2;
43 /** The Constant COMMAND_SIZE. */
44 protected static final byte COMMAND_SIZE = 2;
47 private final CRC8 crc8 = new CRC8();
49 /** The internal payload. */
50 protected byte[] internal_payload;
57 protected abstract byte[] getCommand();
60 * Gets the frame data.
62 * @return the frame data
64 protected abstract byte[] getFrameData();
71 protected abstract byte[] getPayload();
76 * @param buffer the buffer
79 protected byte getCRC8(byte[] buffer) {
81 crc8.update(buffer, 0, buffer.length - 1);
82 return (byte) (crc8.getValue());
86 * @see java.lang.Object#toString()
91 * @see java.lang.Object#toString()
94 public String toString() {
95 StringBuilder sb = new StringBuilder();
97 sb.append(ByteUtil.toString(internal_payload));
104 * @param payload the new payload
106 public void setPayload(byte[] payload) {
107 setInternal_payload(payload);
111 * Gets the internal payload.
113 * @return the internal payload
115 protected byte[] getInternal_payload() {
116 return internal_payload;
120 * Sets the internal payload.
122 * @param internal_payload the new internal payload
124 protected void setInternal_payload(byte[] internal_payload) {
125 this.internal_payload = internal_payload;