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.dali.internal.protocol;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.dali.internal.handler.DaliException;
22 * The {@link DaliFrame} represents a message on the DALI bus.
24 * @author Robert Schmid - Initial contribution
27 public class DaliFrame {
31 public DaliFrame(int bits, byte[] data) throws DaliException {
33 throw new DaliException("Frames must contain at least 1 data bit");
40 d = (d << 8) | Byte.toUnsignedInt(b);
46 throw new DaliException("Initial data must not be negative");
49 if (Math.abs(this.data) >= (1 << this.bits)) {
50 throw new DaliException("Initial data will not fit in the specified number of bits");
58 public byte[] pack() {
59 int remaining = length();
60 List<Byte> bytesList = new ArrayList<Byte>();
62 while (remaining > 0) {
63 bytesList.add((byte) (tmp & 0xff));
65 remaining = remaining - 8;
67 byte[] result = new byte[bytesList.size()];
69 for (byte b : bytesList) {
70 result[bytesList.size() - ++i] = b;