-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve protocol header documentation
Signed-off-by: Jacob Laursen <[email protected]>
- Loading branch information
Showing
7 changed files
with
224 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ain/java/org/openhab/binding/bluetooth/grundfosalpha/internal/protocol/MessageHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.bluetooth.grundfosalpha.internal.protocol; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* This defines protocol header related constants. | ||
* | ||
* @author Jacob Laursen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class MessageHeader { | ||
|
||
/** | ||
* Header length including {@link MessageStartDelimiter} and size byte. | ||
*/ | ||
public static final int LENGTH = 5; | ||
|
||
private static final byte OFFSET_START_DELIMITER = 0; | ||
private static final byte OFFSET_LENGTH = 1; | ||
private static final byte OFFSET_SOURCE_ADDRESS = 2; | ||
private static final byte OFFSET_DESTINATION_ADDRESS = 3; | ||
private static final byte OFFSET_HEADER4 = 4; | ||
|
||
/** | ||
* Address of the controller/client (openHAB). | ||
*/ | ||
private static final byte CONTROLLER_ADDRESS = (byte) 0xe7; | ||
|
||
/** | ||
* Address of the peripheral/server (pump). | ||
*/ | ||
private static final byte PERIPHERAL_ADDRESS = (byte) 0xf8; | ||
|
||
/** | ||
* Last byte in header used for flowhead/power requests/responses. | ||
* Not sure about meaning. | ||
*/ | ||
private static final byte HEADER4_VALUE = (byte) 0x0a; | ||
|
||
/** | ||
* Fill in header for a request. | ||
* | ||
* @param request Request buffer | ||
* @param messageLength The request size excluding {@link MessageStartDelimiter}, size byte and CRC-16 checksum | ||
*/ | ||
public static void setRequestHeader(byte[] request, int messageLength) { | ||
if (request.length < LENGTH) { | ||
throw new IllegalArgumentException("Buffer is too small for header"); | ||
} | ||
|
||
request[OFFSET_START_DELIMITER] = MessageStartDelimiter.Request.getValue(); | ||
request[OFFSET_LENGTH] = (byte) messageLength; | ||
request[OFFSET_SOURCE_ADDRESS] = CONTROLLER_ADDRESS; | ||
request[OFFSET_DESTINATION_ADDRESS] = PERIPHERAL_ADDRESS; | ||
request[OFFSET_HEADER4] = HEADER4_VALUE; | ||
} | ||
|
||
/** | ||
* Check if this packet is the first packet in a response payload. | ||
* | ||
* @param packet The packet to inspect | ||
* @return true if determined to be first packet, otherwise false | ||
*/ | ||
public static boolean isInitialResponsePacket(byte[] packet) { | ||
return packet.length >= LENGTH && packet[OFFSET_START_DELIMITER] == MessageStartDelimiter.Reply.getValue() | ||
&& packet[OFFSET_SOURCE_ADDRESS] == PERIPHERAL_ADDRESS | ||
&& packet[OFFSET_DESTINATION_ADDRESS] == CONTROLLER_ADDRESS && packet[OFFSET_HEADER4] == HEADER4_VALUE; | ||
} | ||
|
||
/** | ||
* Get total size of message including {@link MessageStartDelimiter}, size byte and CRC-16 checksum. | ||
* | ||
* @param header Header bytes (at least) | ||
* @return total size | ||
*/ | ||
public static int getTotalSize(byte[] header) { | ||
return ((byte) header[MessageHeader.OFFSET_LENGTH]) + 4; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../org/openhab/binding/bluetooth/grundfosalpha/internal/protocol/MessageStartDelimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.bluetooth.grundfosalpha.internal.protocol; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* This defines the start delimiters for different kinds of messages. | ||
* | ||
* @author Jacob Laursen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public enum MessageStartDelimiter { | ||
Reply((byte) 0x24), | ||
Message((byte) 0x26), | ||
Request((byte) 0x27); | ||
|
||
private final byte value; | ||
|
||
MessageStartDelimiter(byte value) { | ||
this.value = value; | ||
} | ||
|
||
public byte getValue() { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...t/java/org/openhab/binding/bluetooth/grundfosalpha/internal/protocol/MessageTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.bluetooth.grundfosalpha.internal.protocol; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.junit.jupiter.api.Test; | ||
import org.openhab.core.util.HexUtils; | ||
|
||
/** | ||
* Tests for {@link MessageType}. | ||
* | ||
* @author Jacob Laursen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class MessageTypeTest { | ||
@Test | ||
void requestFlowHead() { | ||
String expected = "27 07 E7 F8 0A 03 5D 01 21 52 1F"; | ||
assertThat(HexUtils.bytesToHex(MessageType.FlowHead.request(), " "), is(expected)); | ||
} | ||
|
||
@Test | ||
void requestPower() { | ||
String expected = "27 07 E7 F8 0A 03 57 00 45 8A CD"; | ||
assertThat(HexUtils.bytesToHex(MessageType.Power.request(), " "), is(expected)); | ||
} | ||
} |