Skip to content

Commit

Permalink
Throw an exception on master-master commands with slave addresses…
Browse files Browse the repository at this point in the history
… that have no master address pair
  • Loading branch information
Christian S committed Jan 29, 2020
1 parent d5cba01 commit 7ac05e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/main/java/de/csdev/ebus/command/EBusCommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,21 @@ public static ByteBuffer buildMasterTelegram(IEBusCommandMethod commandMethod, B
if (commandMethod.getType().equals(Type.BROADCAST)) {
if (target != EBusConsts.BROADCAST_ADDRESS) {
targetChecked = EBusConsts.BROADCAST_ADDRESS;
logger.warn("Replace target address {} with valid broadcast address FE !",
logger.warn("Replace target address {} with valid broadcast address 0xFE !",
EBusUtils.toHexDumpString(target));
}
} else if (commandMethod.getType().equals(Type.MASTER_MASTER)) {
if (!EBusUtils.isMasterAddress(target)) {
targetChecked = EBusUtils.getMasterAddress(target);
logger.warn("Replace slave target address {} with valid master address {}!",
EBusUtils.toHexDumpString(target), EBusUtils.toHexDumpString(targetChecked));

if (targetChecked == null) {
throw new IllegalArgumentException(String.format(
"Cannot replace the slave address 0x%s with a master address because it is a slave address without a master address.",
EBusUtils.toHexDumpString(target)));
} else {
logger.warn("Replace slave target address {} with valid master address {}!",
EBusUtils.toHexDumpString(target), EBusUtils.toHexDumpString(targetChecked));
}
}

} else if (commandMethod.getType().equals(Type.MASTER_SLAVE)) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/de/csdev/ebus/utils/EBusUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public static byte crc8_tab(byte data, byte crcInit) {
*/
static public Byte getMasterAddress(byte slaveAddress) {

if (slaveAddress == EBusConsts.ESCAPE || slaveAddress == EBusConsts.SYN) {
return null;
}

if (!isMasterAddress(slaveAddress)) {
byte masterAddress = (byte) (slaveAddress == (byte) 0x04 ? (byte) 0xFF : slaveAddress - 5);
if (isMasterAddress(masterAddress)) {
Expand Down Expand Up @@ -179,6 +183,10 @@ static public Byte getSlaveAddress(byte masterAddress) {
*/
public static boolean isMasterAddress(byte address) {

if (!isValidAddress(address)) {
return false;
}

byte addr = (byte) ((byte) (address >>> 4) & (byte) 0x0F);
byte prio = (byte) (address & (byte) 0x0F);

Expand All @@ -198,12 +206,12 @@ public static boolean isMasterAddress(byte address) {

/**
* Check if the address is a valid slave address.
*
*
* @param address
* @return
*/
public static boolean isSlaveAddress(byte address) {
return address == EBusConsts.BROADCAST_ADDRESS ? false : !isMasterAddress(address);
return isValidAddress(address) && !isMasterAddress(address);
}

/**
Expand Down

0 comments on commit 7ac05e4

Please sign in to comment.