2 * Copyright (c) 2010-2022 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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.dali.internal.handler.DaliException;
19 * The {@link DaliAddress} represents an address on the DALI bus.
21 * @author Robert Schmid - Initial contribution
24 public abstract class DaliAddress {
25 private DaliAddress() {
28 protected abstract <T extends DaliFrame> T addToFrame(T frame) throws DaliException;
30 public static DaliAddress createShortAddress(int address) throws DaliException {
31 if (address < 0 || address > 63) {
32 throw new DaliException("address must be in the range 0..63");
34 return new DaliAddress() {
36 protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
37 if (frame.length() == 16) {
38 frame.data &= ~(1 << 15); // unset bit 15
39 frame.data |= ((address & 0b11111) << 9);
40 } else if (frame.length() == 24) {
41 frame.data &= ~(1 << 23); // unset bit 23
42 frame.data |= ((address & 0b11111) << 17);
44 throw new DaliException("Unsupported frame size");
51 public static DaliAddress createBroadcastAddress() {
52 return new DaliAddress() {
54 protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
55 if (frame.length() == 16) {
56 frame.data |= 0x7f << 9;
57 } else if (frame.length() == 24) {
58 frame.data |= 0x7f << 17;
60 throw new DaliException("Unsupported frame size");
67 public static DaliAddress createBroadcastUnaddressedAddress() {
68 return new DaliAddress() {
70 protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
71 if (frame.length() == 16) {
72 frame.data |= 0x7e << 9;
73 } else if (frame.length() == 24) {
74 frame.data |= 0x7e << 17;
76 throw new DaliException("Unsupported frame size");
83 public static DaliAddress createGroupAddress(int address) throws DaliException {
84 if (address < 0 || address > 31) {
85 throw new DaliException("address must be in the range 0..31");
87 return new DaliAddress() {
89 protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
90 if (frame.length() == 16) {
92 throw new DaliException("Groups 16..31 are not supported in 16-bit forward frames");
94 frame.data |= ((0x4 << 3) & (address & 0b111)) << 9;
95 } else if (frame.length() == 24) {
96 frame.data |= ((0x2 << 4) & (address & 0b1111)) << 17;
98 throw new DaliException("Unsupported frame size");