Skip to content

Commit

Permalink
Copy SuplaDataPacket.java because it has extra sulpa tag in proto.h
Browse files Browse the repository at this point in the history
  • Loading branch information
magx2 committed May 8, 2024
1 parent f86d8c8 commit bbdd5bd
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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<SuplaDataPacket> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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<SuplaDataPacket> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends ProtoToSend> extends Encoder<T> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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:
* <pre>
* 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;
* </pre>
*/
@lombok.EqualsAndHashCode
@lombok.ToString
public class SuplaDataPacket implements ProtoWithSize {
/**
* unsigned char
*/
public final short version;
/**
* Request/Response ID
* <p>
* 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
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SuplaDataPacket> {
private final RandomSupla randomSupla;
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -95,7 +94,6 @@ private SuplaDataPacket encodeProto(ProtoToSend proto) {
final Encoder<ProtoToSend> encoder = encoderFactory.getEncoder(proto);
byte[] encode = encoder.encode(proto);
return new SuplaDataPacket(
SUPLA_TAG,
(short) 5,
msgId.getAndIncrement(),
proto.callType().getValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit bbdd5bd

Please sign in to comment.