From bbdd5bded836cae0234690043037c90b373a5a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Grze=C5=9Blowski?= Date: Wed, 8 May 2024 22:46:22 +0200 Subject: [PATCH] Copy SuplaDataPacket.java because it has extra sulpa tag in proto.h --- .../api/decoders/SuplaDataPacketDecoder.java | 32 +++++++++ .../api/encoders/SuplaDataPacketEncoder.java | 22 ++++++ .../encoders/ToSuplaDataPacketEncoder.java | 4 +- .../protocol/api/structs/SuplaDataPacket.java | 71 +++++++++++++++++++ .../SuplaDataPacketRandomizer.java | 2 - .../jsupla/server/netty/NettyChannel.java | 2 - .../server/netty/SuplaDataPacketDecoder.java | 2 +- 7 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/decoders/SuplaDataPacketDecoder.java create mode 100644 protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/SuplaDataPacketEncoder.java create mode 100644 protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/structs/SuplaDataPacket.java diff --git a/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/decoders/SuplaDataPacketDecoder.java b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/decoders/SuplaDataPacketDecoder.java new file mode 100644 index 00000000..1a1c1df4 --- /dev/null +++ b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/decoders/SuplaDataPacketDecoder.java @@ -0,0 +1,32 @@ +package pl.grzeslowski.jsupla.protocol.api.decoders; + +import lombok.val; +import pl.grzeslowski.jsupla.protocol.api.structs.SuplaDataPacket; + +import static pl.grzeslowski.jsupla.protocol.api.JavaConsts.BYTE_SIZE; +import static pl.grzeslowski.jsupla.protocol.api.JavaConsts.INT_SIZE; + +@lombok.NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) +public class SuplaDataPacketDecoder implements ProtoWithSizeDecoder { + public static final SuplaDataPacketDecoder INSTANCE = new SuplaDataPacketDecoder(); + + @Override + public SuplaDataPacket decode(byte[] bytes, int offset) { + val version = PrimitiveDecoder.INSTANCE.parseUnsignedByte(bytes, offset); + offset += BYTE_SIZE; + + val rrId = PrimitiveDecoder.INSTANCE.parseUnsignedInt(bytes, offset); + offset += INT_SIZE; + + val callId = PrimitiveDecoder.INSTANCE.parseUnsignedInt(bytes, offset); + offset += INT_SIZE; + + val dataSize = PrimitiveDecoder.INSTANCE.parseUnsignedInt(bytes, offset); + offset += INT_SIZE; + + val data = PrimitiveDecoder.INSTANCE.copyOfRangeByte(bytes, offset, offset + (int) dataSize); + offset += dataSize * BYTE_SIZE; + + return new SuplaDataPacket(version, rrId, callId, dataSize, data); + } +} diff --git a/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/SuplaDataPacketEncoder.java b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/SuplaDataPacketEncoder.java new file mode 100644 index 00000000..e039e41a --- /dev/null +++ b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/SuplaDataPacketEncoder.java @@ -0,0 +1,22 @@ +package pl.grzeslowski.jsupla.protocol.api.encoders; + +import pl.grzeslowski.jsupla.protocol.api.structs.SuplaDataPacket; + +@lombok.NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) +public class SuplaDataPacketEncoder implements ProtoWithSizeEncoder { + public static final SuplaDataPacketEncoder INSTANCE = new SuplaDataPacketEncoder(); + + @Override + public byte[] encode(SuplaDataPacket proto) { + final byte[] bytes = new byte[proto.size()]; + int offset = 0; + + offset += PrimitiveEncoder.INSTANCE.writeUnsignedByte(proto.version, bytes, offset); + offset += PrimitiveEncoder.INSTANCE.writeUnsignedInt(proto.rrId, bytes, offset); + offset += PrimitiveEncoder.INSTANCE.writeUnsignedInt(proto.callId, bytes, offset); + offset += PrimitiveEncoder.INSTANCE.writeUnsignedInt(proto.dataSize, bytes, offset); + offset += PrimitiveEncoder.INSTANCE.writeByteArray(proto.data, bytes, offset); + + return bytes; + } +} diff --git a/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/ToSuplaDataPacketEncoder.java b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/ToSuplaDataPacketEncoder.java index 61ad054f..f3cf80fc 100644 --- a/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/ToSuplaDataPacketEncoder.java +++ b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/ToSuplaDataPacketEncoder.java @@ -3,11 +3,9 @@ import pl.grzeslowski.jsupla.protocol.api.structs.SuplaDataPacket; import pl.grzeslowski.jsupla.protocol.api.types.ProtoToSend; -import static pl.grzeslowski.jsupla.protocol.api.consts.ProtoConsts.SUPLA_TAG; - public interface ToSuplaDataPacketEncoder extends Encoder { default SuplaDataPacket encode(T proto, short version, long rrId) { final byte[] data = encode(proto); - return new SuplaDataPacket(SUPLA_TAG, version, rrId, proto.callType().getValue(), data.length, data); + return new SuplaDataPacket(version, rrId, proto.callType().getValue(), data.length, data); } } diff --git a/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/structs/SuplaDataPacket.java b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/structs/SuplaDataPacket.java new file mode 100644 index 00000000..bce24213 --- /dev/null +++ b/protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/structs/SuplaDataPacket.java @@ -0,0 +1,71 @@ +package pl.grzeslowski.jsupla.protocol.api.structs; + +import pl.grzeslowski.jsupla.protocol.api.types.ProtoWithSize; + +import static pl.grzeslowski.jsupla.protocol.api.JavaConsts.*; +import static pl.grzeslowski.jsupla.protocol.api.Preconditions.checkArrayLength; +import static pl.grzeslowski.jsupla.protocol.api.Preconditions.unsigned; + +/** + * Original code: + *
+ * typedef struct {
+ * char tag[SUPLA_TAG_SIZE];
+ * unsigned char version;
+ * unsigned _supla_int_t rr_id;  // Request/Response ID
+ * unsigned _supla_int_t call_id;
+ * unsigned _supla_int_t data_size;
+ * char data[SUPLA_MAX_DATA_SIZE];  // Last variable in struct!
+ * } TSuplaDataPacket;
+ * 
+ */ +@lombok.EqualsAndHashCode +@lombok.ToString +public class SuplaDataPacket implements ProtoWithSize { + /** + * unsigned char + */ + public final short version; + /** + * Request/Response ID + *

+ * unsigned _supla_int_t + */ + public final long rrId; + /** + * unsigned _supla_int_t + */ + public final long callId; + /** + * unsigned _supla_int_t + */ + public final long dataSize; + /** + * Last variable in struct! + */ + public final byte[] data; + + public SuplaDataPacket(short version, + long rrId, + long callId, + long dataSize, + byte[] data) { + this.version = unsigned(version); + this.rrId = unsigned(rrId); + this.callId = unsigned(callId); + this.dataSize = unsigned(dataSize); + this.data = checkArrayLength(data, (int) dataSize); + } + + /* no call type */ + + @Override + public int size() { + return CHAR_SIZE // version + + INT_SIZE // rrId + + INT_SIZE // callId + + INT_SIZE // dataSize + + (int) dataSize * BYTE_SIZE // data + ; + } +} diff --git a/protocol/src/test/java/pl/grzeslowski/jsupla/protocol/common/randomizers/SuplaDataPacketRandomizer.java b/protocol/src/test/java/pl/grzeslowski/jsupla/protocol/common/randomizers/SuplaDataPacketRandomizer.java index bb89083b..39867145 100644 --- a/protocol/src/test/java/pl/grzeslowski/jsupla/protocol/common/randomizers/SuplaDataPacketRandomizer.java +++ b/protocol/src/test/java/pl/grzeslowski/jsupla/protocol/common/randomizers/SuplaDataPacketRandomizer.java @@ -5,7 +5,6 @@ import pl.grzeslowski.jsupla.protocol.common.RandomSupla; import static pl.grzeslowski.jsupla.protocol.api.consts.ProtoConsts.SUPLA_MAX_DATA_SIZE; -import static pl.grzeslowski.jsupla.protocol.api.consts.ProtoConsts.SUPLA_TAG; public class SuplaDataPacketRandomizer implements Randomizer { private final RandomSupla randomSupla; @@ -18,7 +17,6 @@ public SuplaDataPacketRandomizer(final RandomSupla randomSupla) { public SuplaDataPacket getRandomValue() { final int dataSize = randomSupla.nextPositiveInt(SUPLA_MAX_DATA_SIZE); return new SuplaDataPacket( - SUPLA_TAG, randomSupla.nextUnsignedByte(), randomSupla.nextUnsignedInt(), randomSupla.nextLong(100), diff --git a/server/src/main/java/pl/grzeslowski/jsupla/server/netty/NettyChannel.java b/server/src/main/java/pl/grzeslowski/jsupla/server/netty/NettyChannel.java index 9d929e0f..4e86b96f 100644 --- a/server/src/main/java/pl/grzeslowski/jsupla/server/netty/NettyChannel.java +++ b/server/src/main/java/pl/grzeslowski/jsupla/server/netty/NettyChannel.java @@ -23,7 +23,6 @@ import java.util.concurrent.atomic.AtomicLong; import static java.util.Objects.requireNonNull; -import static pl.grzeslowski.jsupla.protocol.api.consts.ProtoConsts.SUPLA_TAG; @Slf4j public final class NettyChannel implements Channel { @@ -95,7 +94,6 @@ private SuplaDataPacket encodeProto(ProtoToSend proto) { final Encoder encoder = encoderFactory.getEncoder(proto); byte[] encode = encoder.encode(proto); return new SuplaDataPacket( - SUPLA_TAG, (short) 5, msgId.getAndIncrement(), proto.callType().getValue(), diff --git a/server/src/main/java/pl/grzeslowski/jsupla/server/netty/SuplaDataPacketDecoder.java b/server/src/main/java/pl/grzeslowski/jsupla/server/netty/SuplaDataPacketDecoder.java index 4525e8b2..0f7af05b 100644 --- a/server/src/main/java/pl/grzeslowski/jsupla/server/netty/SuplaDataPacketDecoder.java +++ b/server/src/main/java/pl/grzeslowski/jsupla/server/netty/SuplaDataPacketDecoder.java @@ -54,6 +54,6 @@ private SuplaDataPacket readTSuplaDataPacket(ByteBuf in) { byte[] data = new byte[Math.toIntExact(dataSize)]; in.readBytes(data); - return new SuplaDataPacket(SUPLA_TAG, version, rrId, callType, dataSize, data); + return new SuplaDataPacket(version, rrId, callType, dataSize, data); } }